| 1 |
from django.db import models |
| 2 |
|
| 3 |
|
| 4 |
class City(models.Model): |
| 5 |
id = models.BigAutoField(primary_key=True) |
| 6 |
name = models.CharField(max_length=50) |
| 7 |
|
| 8 |
|
| 9 |
class Country(models.Model): |
| 10 |
id = models.SmallAutoField(primary_key=True) |
| 11 |
name = models.CharField(max_length=50) |
| 12 |
|
| 13 |
|
| 14 |
class District(models.Model): |
| 15 |
city = models.ForeignKey(City, models.CASCADE, primary_key=True) |
| 16 |
name = models.CharField(max_length=50) |
| 17 |
|
| 18 |
|
| 19 |
class Reporter(models.Model): |
| 20 |
first_name = models.CharField(max_length=30) |
| 21 |
last_name = models.CharField(max_length=30) |
| 22 |
email = models.EmailField() |
| 23 |
facebook_user_id = models.BigIntegerField(null=True) |
| 24 |
raw_data = models.BinaryField(null=True) |
| 25 |
small_int = models.SmallIntegerField() |
| 26 |
interval = models.DurationField() |
| 27 |
|
| 28 |
class Meta: |
| 29 |
unique_together = ("first_name", "last_name") |
| 30 |
|
| 31 |
|
| 32 |
class Article(models.Model): |
| 33 |
headline = models.CharField(max_length=100) |
| 34 |
pub_date = models.DateField() |
| 35 |
body = models.TextField(default="") |
| 36 |
reporter = models.ForeignKey(Reporter, models.CASCADE) |
| 37 |
response_to = models.ForeignKey("self", models.SET_NULL, null=True) |
| 38 |
unmanaged_reporters = models.ManyToManyField( |
| 39 |
Reporter, through="ArticleReporter", related_name="+" |
| 40 |
) |
| 41 |
|
| 42 |
class Meta: |
| 43 |
ordering = ("headline",) |
| 44 |
indexes = [ |
| 45 |
models.Index(fields=["headline", "pub_date"]), |
| 46 |
models.Index(fields=["headline", "response_to", "pub_date", "reporter"]), |
| 47 |
] |
| 48 |
|
| 49 |
|
| 50 |
class ArticleReporter(models.Model): |
| 51 |
article = models.ForeignKey(Article, models.CASCADE) |
| 52 |
reporter = models.ForeignKey(Reporter, models.CASCADE) |
| 53 |
|
| 54 |
class Meta: |
| 55 |
managed = False |
| 56 |
|
| 57 |
|
| 58 |
class Comment(models.Model): |
| 59 |
ref = models.UUIDField(unique=True) |
| 60 |
article = models.ForeignKey(Article, models.CASCADE, db_index=True) |
| 61 |
email = models.EmailField() |
| 62 |
pub_date = models.DateTimeField() |
| 63 |
body = models.TextField() |
| 64 |
|
| 65 |
class Meta: |
| 66 |
constraints = [ |
| 67 |
models.UniqueConstraint( |
| 68 |
fields=["article", "email", "pub_date"], |
| 69 |
name="article_email_pub_date_uniq", |
| 70 |
), |
| 71 |
] |
| 72 |
indexes = [ |
| 73 |
models.Index(fields=["email", "pub_date"], name="email_pub_date_idx"), |
| 74 |
] |
| 75 |
|
| 76 |
|
| 77 |
class CheckConstraintModel(models.Model): |
| 78 |
up_votes = models.PositiveIntegerField() |
| 79 |
voting_number = models.PositiveIntegerField(unique=True) |
| 80 |
|
| 81 |
class Meta: |
| 82 |
required_db_features = { |
| 83 |
"supports_table_check_constraints", |
| 84 |
} |
| 85 |
constraints = [ |
| 86 |
models.CheckConstraint( |
| 87 |
name="up_votes_gte_0_check", condition=models.Q(up_votes__gte=0) |
| 88 |
), |
| 89 |
] |
| 90 |
|
| 91 |
|
| 92 |
class UniqueConstraintConditionModel(models.Model): |
| 93 |
name = models.CharField(max_length=255) |
| 94 |
color = models.CharField(max_length=32, null=True) |
| 95 |
|
| 96 |
class Meta: |
| 97 |
required_db_features = {"supports_partial_indexes"} |
| 98 |
constraints = [ |
| 99 |
models.UniqueConstraint( |
| 100 |
fields=["name"], |
| 101 |
name="cond_name_without_color_uniq", |
| 102 |
condition=models.Q(color__isnull=True), |
| 103 |
), |
| 104 |
] |
| 105 |
|
| 106 |
|
| 107 |
class DbCommentModel(models.Model): |
| 108 |
name = models.CharField(max_length=15, db_comment="'Name' column comment") |
| 109 |
|
| 110 |
class Meta: |
| 111 |
db_table_comment = "Custom table comment" |
| 112 |
required_db_features = {"supports_comments"} |