| 1 |
""" |
| 2 |
Bare-bones model |
| 3 |
|
| 4 |
This is a basic model with only two non-primary-key fields. |
| 5 |
""" |
| 6 |
|
| 7 |
import uuid |
| 8 |
|
| 9 |
from django.db import models |
| 10 |
|
| 11 |
|
| 12 |
class Article(models.Model): |
| 13 |
headline = models.CharField(max_length=100, default="Default headline") |
| 14 |
pub_date = models.DateTimeField() |
| 15 |
|
| 16 |
class Meta: |
| 17 |
ordering = ("pub_date", "headline") |
| 18 |
|
| 19 |
def __str__(self): |
| 20 |
return self.headline |
| 21 |
|
| 22 |
|
| 23 |
class FeaturedArticle(models.Model): |
| 24 |
article = models.OneToOneField(Article, models.CASCADE, related_name="featured") |
| 25 |
|
| 26 |
|
| 27 |
class ArticleSelectOnSave(Article): |
| 28 |
class Meta: |
| 29 |
proxy = True |
| 30 |
select_on_save = True |
| 31 |
|
| 32 |
|
| 33 |
class SelfRef(models.Model): |
| 34 |
selfref = models.ForeignKey( |
| 35 |
"self", |
| 36 |
models.SET_NULL, |
| 37 |
null=True, |
| 38 |
blank=True, |
| 39 |
related_name="+", |
| 40 |
) |
| 41 |
article = models.ForeignKey(Article, models.SET_NULL, null=True, blank=True) |
| 42 |
article_cited = models.ForeignKey( |
| 43 |
Article, models.SET_NULL, null=True, blank=True, related_name="cited" |
| 44 |
) |
| 45 |
|
| 46 |
def __str__(self): |
| 47 |
# This method intentionally doesn't work for all cases - part |
| 48 |
# of the test for ticket #20278 |
| 49 |
return SelfRef.objects.get(selfref=self).pk |
| 50 |
|
| 51 |
|
| 52 |
class PrimaryKeyWithDefault(models.Model): |
| 53 |
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4) |
| 54 |
|
| 55 |
|
| 56 |
class PrimaryKeyWithDbDefault(models.Model): |
| 57 |
uuid = models.IntegerField(primary_key=True, db_default=1) |
| 58 |
|
| 59 |
|
| 60 |
class PrimaryKeyWithFalseyDefault(models.Model): |
| 61 |
uuid = models.IntegerField(primary_key=True, default=0) |
| 62 |
|
| 63 |
|
| 64 |
class PrimaryKeyWithFalseyDbDefault(models.Model): |
| 65 |
uuid = models.IntegerField(primary_key=True, db_default=0) |
| 66 |
|
| 67 |
|
| 68 |
class ChildPrimaryKeyWithDefault(PrimaryKeyWithDefault): |
| 69 |
pass |