File: models.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (585 lines) | stat: -rw-r--r-- 15,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth import get_user_model
from django.db.models import UniqueConstraint
from django.db.models.signals import pre_save

from django_extensions.db.fields import (
    AutoSlugField,
    ModificationDateTimeField,
    RandomCharField,
    ShortUUIDField,
)
from django_extensions.db.fields.json import JSONField
from django_extensions.db.models import ActivatorModel, TimeStampedModel

from .fields import UniqField


class Secret(models.Model):
    name = models.CharField(blank=True, max_length=255, null=True)
    text = models.TextField(blank=True, null=True)

    class Meta:
        app_label = "django_extensions"


class Name(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        app_label = "django_extensions"


class Personality(models.Model):
    description = models.CharField(max_length=50)


class Club(models.Model):
    name = models.CharField(max_length=50)
    created = models.DateTimeField(auto_now=True)


class Note(models.Model):
    note = models.TextField()
    club = models.ForeignKey(Club, null=True, on_delete=models.CASCADE)

    class Meta:
        app_label = "django_extensions"


class Neighborhood(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        app_label = "django_extensions"


class Bank(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        app_label = "django_extensions"


class Person(models.Model):
    name = models.ForeignKey(Name, on_delete=models.CASCADE)
    age = models.PositiveIntegerField()
    children = models.ManyToManyField("self")
    notes = models.ManyToManyField(Note)
    personality = models.OneToOneField(
        Personality,
        null=True,
        on_delete=models.CASCADE,
    )
    clubs = models.ManyToManyField(Club, through="testapp.Membership")
    neighborhood = models.ForeignKey(Neighborhood, on_delete=models.SET_NULL, null=True)
    current_bank = models.ForeignKey(Bank, on_delete=models.PROTECT, null=True)

    class Meta:
        app_label = "django_extensions"


class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    club = models.ForeignKey(Club, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now=True)


class Post(ActivatorModel):
    title = models.CharField(max_length=255)

    class Meta:
        app_label = "django_extensions"


class PostWithTitleOrdering(Post):
    class Meta:
        proxy = True
        ordering = ["title"]


class DummyRelationModel(models.Model):
    class Meta:
        app_label = "django_extensions"


class SecondDummyRelationModel(models.Model):
    class Meta:
        app_label = "django_extensions"


class ThirdDummyRelationModel(models.Model):
    class Meta:
        app_label = "django_extensions"


class PostWithUniqFieldCompat(models.Model):
    """
    django-extensions from version 3.0 officially supports only Django 2.2+ which
    introduced Meta.constraints and suggests using it instead of Meta.unique_together
    this is left only to ensure compatibility with Meta.unique_together
    """

    uniq_field = UniqField(
        max_length=255, boolean_attr=True, non_boolean_attr="non_boolean_attr"
    )
    common_field = models.CharField(max_length=10)
    another_common_field = models.CharField(max_length=10)
    many_to_one_field = models.ForeignKey(DummyRelationModel, on_delete=models.CASCADE)
    one_to_one_field = models.OneToOneField(
        SecondDummyRelationModel, on_delete=models.CASCADE
    )
    many_to_many_field = models.ManyToManyField(
        ThirdDummyRelationModel, related_name="posts_with_uniq"
    )

    class Meta:
        app_label = "django_extensions"
        unique_together = (
            "common_field",
            "uniq_field",
        )


class ReverseModelCompat(models.Model):
    post_field = models.ForeignKey(
        PostWithUniqFieldCompat, related_name="reverse_models", on_delete=models.CASCADE
    )

    class Meta:
        app_label = "django_extensions"


class InheritedFromPostWithUniqFieldCompat(PostWithUniqFieldCompat):
    new_field = models.CharField(max_length=10)

    class Meta:
        app_label = "django_extensions"


class PostWithUniqField(models.Model):
    uniq_field = UniqField(
        max_length=255, boolean_attr=True, non_boolean_attr="non_boolean_attr"
    )
    common_field = models.CharField(max_length=10)
    another_common_field = models.CharField(max_length=10)
    many_to_one_field = models.ForeignKey(DummyRelationModel, on_delete=models.CASCADE)
    one_to_one_field = models.OneToOneField(
        SecondDummyRelationModel, on_delete=models.CASCADE
    )
    many_to_many_field = models.ManyToManyField(
        ThirdDummyRelationModel, related_name="posts22_with_uniq"
    )

    class Meta:
        app_label = "django_extensions"
        constraints = [
            models.UniqueConstraint(
                fields=("common_field", "uniq_field"), name="unique_common_uniq_pair"
            ),
            models.CheckConstraint(
                check=~models.Q(common_field=models.F("another_common_field")),
                name="common_and_another_common_differ",
            ),
        ]


class ReverseModel(models.Model):
    post_field = models.ForeignKey(
        PostWithUniqField, related_name="reverse_models", on_delete=models.CASCADE
    )

    class Meta:
        app_label = "django_extensions"


class InheritedFromPostWithUniqField(PostWithUniqField):
    new_field = models.CharField(max_length=10)

    class Meta:
        app_label = "django_extensions"


class AbstractInheritanceTestModelParent(models.Model):
    my_field_that_my_child_will_inherit = models.BooleanField()

    class Meta:
        app_label = "django_extensions"
        abstract = True


class AbstractInheritanceTestModelChild(AbstractInheritanceTestModelParent):
    class Meta:
        app_label = "django_extensions"


class SluggedTestModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title")

    class Meta:
        app_label = "django_extensions"


class SluggedWithConstraintsTestModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title")
    category = models.CharField(max_length=20, null=True)

    class Meta:
        app_label = "django_extensions"
        constraints = [
            UniqueConstraint(
                fields=["slug", "category"],
                name="unique_slug_and_category",
            ),
        ]


class SluggedWithUniqueTogetherTestModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title")
    category = models.CharField(max_length=20, null=True)

    class Meta:
        app_label = "django_extensions"
        unique_together = ["slug", "category"]


class OverridedFindUniqueAutoSlugField(AutoSlugField):
    def find_unique(self, model_instance, field, iterator, *args):
        self.overrided = True
        return super().find_unique(model_instance, field, iterator, *args)


class OverridedFindUniqueModel(models.Model):
    title = models.CharField(max_length=42)
    slug = OverridedFindUniqueAutoSlugField(populate_from="title")


class CustomFuncSluggedTestModel(models.Model):
    def slugify_function(self, content):
        return content.upper()

    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title")

    class Meta:
        app_label = "django_extensions"


class CustomFuncPrecedenceSluggedTestModel(models.Model):
    def custom_slug_one(self, content):
        return content.upper()

    def custom_slug_two(content):
        return content.lower()

    slugify_function = custom_slug_one

    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title", slugify_function=custom_slug_two)

    class Meta:
        app_label = "django_extensions"


class ChildSluggedTestModel(SluggedTestModel):
    class Meta:
        app_label = "django_extensions"


class SluggedTestNoOverwriteOnAddModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="title", overwrite_on_add=False)

    class Meta:
        app_label = "django_extensions"


def get_readable_title(instance):
    return "The title is {}".format(instance.title)


class ModelMethodSluggedTestModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from="get_readable_title")

    class Meta:
        app_label = "django_extensions"

    def get_readable_title(self):
        return get_readable_title(self)


class FunctionSluggedTestModel(models.Model):
    title = models.CharField(max_length=42)
    slug = AutoSlugField(populate_from=get_readable_title)

    class Meta:
        app_label = "django_extensions"


class FKSluggedTestModel(models.Model):
    related_field = models.ForeignKey(SluggedTestModel, on_delete=models.CASCADE)
    slug = AutoSlugField(populate_from="related_field__title")

    class Meta:
        app_label = "django_extensions"


class FKSluggedTestModelCallable(models.Model):
    related_field = models.ForeignKey(
        ModelMethodSluggedTestModel, on_delete=models.CASCADE
    )
    slug = AutoSlugField(populate_from="related_field__get_readable_title")

    class Meta:
        app_label = "django_extensions"


class JSONFieldTestModel(models.Model):
    a = models.IntegerField()
    j_field = JSONField()

    class Meta:
        app_label = "django_extensions"


class ShortUUIDTestModel_field(models.Model):
    a = models.IntegerField()
    uuid_field = ShortUUIDField()

    class Meta:
        app_label = "django_extensions"


class ShortUUIDTestModel_pk(models.Model):
    uuid_field = ShortUUIDField(primary_key=True)

    class Meta:
        app_label = "django_extensions"


class ShortUUIDTestAgregateModel(ShortUUIDTestModel_pk):
    a = models.IntegerField()

    class Meta:
        app_label = "django_extensions"


class ShortUUIDTestManyToManyModel(ShortUUIDTestModel_pk):
    many = models.ManyToManyField(ShortUUIDTestModel_field)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModel(models.Model):
    random_char_field = RandomCharField(length=8, unique=False)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelUnique(models.Model):
    random_char_field = RandomCharField(length=8, unique=True)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelAlphaDigits(models.Model):
    random_char_field = RandomCharField(length=8, unique=True)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelLowercaseAlphaDigits(models.Model):
    random_char_field = RandomCharField(length=8, lowercase=True)

    class Meta:
        app_label = "django_extensions"
        verbose_name = "lowercase alpha digits"


class RandomCharTestModelUppercaseAlphaDigits(models.Model):
    random_char_field = RandomCharField(length=8, uppercase=True)

    class Meta:
        app_label = "django_extensions"
        verbose_name = "uppercase alpha digits"


class RandomCharTestModelLowercase(models.Model):
    random_char_field = RandomCharField(length=8, lowercase=True, include_digits=False)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelUppercase(models.Model):
    random_char_field = RandomCharField(length=8, uppercase=True, include_digits=False)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelAlpha(models.Model):
    random_char_field = RandomCharField(length=8, include_digits=False)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelDigits(models.Model):
    random_char_field = RandomCharField(length=8, include_alpha=False)

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelPunctuation(models.Model):
    random_char_field = RandomCharField(
        length=8,
        include_punctuation=True,
        include_digits=False,
        include_alpha=False,
    )

    class Meta:
        app_label = "django_extensions"


class RandomCharTestModelUniqueTogether(models.Model):
    random_char_field = RandomCharField(length=8)
    common_field = models.CharField(max_length=10)

    class Meta:
        app_label = "django_extensions"
        unique_together = ("random_char_field", "common_field")


class TimestampedTestModel(TimeStampedModel):
    class Meta:
        app_label = "django_extensions"


class UnicodeVerboseNameModel(models.Model):
    cafe = models.IntegerField(verbose_name="café")
    parent_cafe = models.ForeignKey(
        "self",
        related_name="children",
        on_delete=models.CASCADE,
        verbose_name="café latte",
    )

    class Meta:
        app_label = "django_extensions"
        verbose_name = "café unicode model"


class Permission(models.Model):
    text = models.CharField(max_length=32)
    person = models.ForeignKey(Person, on_delete=models.CASCADE)


class UniqueTestAppModel(models.Model):
    global_id = models.CharField(max_length=32, unique=True)


class SqlDiff(models.Model):
    number = models.CharField(max_length=40, null=True, verbose_name="Chargennummer")
    creator = models.CharField(max_length=20, null=True, blank=True)

    class Meta:
        indexes = [models.Index(fields=["number", "creator"])]


class SqlDiffIndexes(models.Model):
    first = models.CharField(max_length=40, null=True, verbose_name="Chargennummer")
    second = models.CharField(max_length=20, null=True, blank=True)

    class Meta:
        indexes = [
            models.Index(fields=["first", "second"]),
        ]


class SqlDiffUniqueTogether(models.Model):
    aaa = models.CharField(max_length=20)
    bbb = models.CharField(max_length=20)

    class Meta:
        unique_together = ["aaa", "bbb"]


class Photo(models.Model):
    photo = models.FileField()


class CustomModelModificationDateTimeField(models.Model):
    field_to_update = models.BooleanField(default=True)
    custom_modified = ModificationDateTimeField()

    class Meta:
        app_label = "django_extensions"


class ModelModificationDateTimeField(models.Model):
    field_to_update = models.BooleanField(default=True)
    modified = ModificationDateTimeField()

    class Meta:
        app_label = "django_extensions"


class DisabledUpdateModelModificationDateTimeField(models.Model):
    field_to_update = models.BooleanField(default=True)
    modified = ModificationDateTimeField()

    update_modified = False

    class Meta:
        app_label = "django_extensions"


class HasOwnerModel(models.Model):
    content = models.TextField(default="")
    owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    class Meta:
        app_label = "django_extensions"


class MultipleFieldsAndMethods(models.Model):
    char_field = models.CharField(max_length=10)
    integer_field = models.IntegerField()
    foreign_key_field = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    def has_self_only(self):
        pass

    def has_one_extra_argument(self, arg_one):
        pass

    def has_two_extra_arguments(self, arg_one, arg_two):
        pass

    def has_args_kwargs(self, *args, **kwargs):
        pass

    def has_defaults(self, one=1, two="Two", true=True, false=False, none=None):
        pass

    class Meta:
        app_label = "django_extensions"


def dummy_handler(sender, instance, **kwargs):
    pass


pre_save.connect(dummy_handler, sender=HasOwnerModel)