2
0

models.py 3.3 KB

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