models.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from django.apps.registry import Apps
  2. from django.db import models
  3. # Because we want to test creation and deletion of these as separate things,
  4. # these models are all inserted into a separate Apps so the main test
  5. # runner doesn't migrate them.
  6. new_apps = Apps()
  7. class Author(models.Model):
  8. name = models.CharField(max_length=255)
  9. height = models.PositiveIntegerField(null=True, blank=True)
  10. weight = models.IntegerField(null=True, blank=True)
  11. uuid = models.UUIDField(null=True)
  12. class Meta:
  13. apps = new_apps
  14. class AuthorCharFieldWithIndex(models.Model):
  15. char_field = models.CharField(max_length=31, db_index=True)
  16. class Meta:
  17. apps = new_apps
  18. class AuthorTextFieldWithIndex(models.Model):
  19. text_field = models.TextField(db_index=True)
  20. class Meta:
  21. apps = new_apps
  22. class AuthorWithDefaultHeight(models.Model):
  23. name = models.CharField(max_length=255)
  24. height = models.PositiveIntegerField(null=True, blank=True, default=42)
  25. class Meta:
  26. apps = new_apps
  27. class AuthorWithEvenLongerName(models.Model):
  28. name = models.CharField(max_length=255)
  29. height = models.PositiveIntegerField(null=True, blank=True)
  30. class Meta:
  31. apps = new_apps
  32. class AuthorWithIndexedName(models.Model):
  33. name = models.CharField(max_length=255, db_index=True)
  34. class Meta:
  35. apps = new_apps
  36. class AuthorWithUniqueName(models.Model):
  37. name = models.CharField(max_length=255, unique=True)
  38. class Meta:
  39. apps = new_apps
  40. class AuthorWithUniqueNameAndBirthday(models.Model):
  41. name = models.CharField(max_length=255)
  42. birthday = models.DateField()
  43. class Meta:
  44. apps = new_apps
  45. unique_together = [["name", "birthday"]]
  46. class Book(models.Model):
  47. author = models.ForeignKey(Author, models.CASCADE)
  48. title = models.CharField(max_length=100, db_index=True)
  49. pub_date = models.DateTimeField()
  50. # tags = models.ManyToManyField("Tag", related_name="books")
  51. class Meta:
  52. apps = new_apps
  53. class BookWeak(models.Model):
  54. author = models.ForeignKey(Author, models.CASCADE, db_constraint=False)
  55. title = models.CharField(max_length=100, db_index=True)
  56. pub_date = models.DateTimeField()
  57. class Meta:
  58. apps = new_apps
  59. class BookWithLongName(models.Model):
  60. author_foreign_key_with_really_long_field_name = models.ForeignKey(
  61. AuthorWithEvenLongerName,
  62. models.CASCADE,
  63. )
  64. class Meta:
  65. apps = new_apps
  66. class BookWithO2O(models.Model):
  67. author = models.OneToOneField(Author, models.CASCADE)
  68. title = models.CharField(max_length=100, db_index=True)
  69. pub_date = models.DateTimeField()
  70. class Meta:
  71. apps = new_apps
  72. db_table = "schema_book"
  73. class BookWithSlug(models.Model):
  74. author = models.ForeignKey(Author, models.CASCADE)
  75. title = models.CharField(max_length=100, db_index=True)
  76. pub_date = models.DateTimeField()
  77. slug = models.CharField(max_length=20, unique=True)
  78. class Meta:
  79. apps = new_apps
  80. db_table = "schema_book"
  81. class BookWithoutAuthor(models.Model):
  82. title = models.CharField(max_length=100, db_index=True)
  83. pub_date = models.DateTimeField()
  84. class Meta:
  85. apps = new_apps
  86. db_table = "schema_book"
  87. class BookForeignObj(models.Model):
  88. title = models.CharField(max_length=100, db_index=True)
  89. author_id = models.IntegerField()
  90. class Meta:
  91. apps = new_apps
  92. class IntegerPK(models.Model):
  93. i = models.IntegerField(primary_key=True)
  94. j = models.IntegerField(unique=True)
  95. class Meta:
  96. apps = new_apps
  97. db_table = "INTEGERPK" # uppercase to ensure proper quoting
  98. class Note(models.Model):
  99. info = models.TextField()
  100. address = models.TextField(null=True)
  101. class Meta:
  102. apps = new_apps
  103. class NoteRename(models.Model):
  104. detail_info = models.TextField()
  105. class Meta:
  106. apps = new_apps
  107. db_table = "schema_note"
  108. class Tag(models.Model):
  109. title = models.CharField(max_length=255)
  110. slug = models.SlugField(unique=True)
  111. class Meta:
  112. apps = new_apps
  113. class TagM2MTest(models.Model):
  114. title = models.CharField(max_length=255)
  115. slug = models.SlugField(unique=True)
  116. class Meta:
  117. apps = new_apps
  118. class TagUniqueRename(models.Model):
  119. title = models.CharField(max_length=255)
  120. slug2 = models.SlugField(unique=True)
  121. class Meta:
  122. apps = new_apps
  123. db_table = "schema_tag"
  124. # Based on tests/reserved_names/models.py
  125. class Thing(models.Model):
  126. when = models.CharField(max_length=1, primary_key=True)
  127. class Meta:
  128. apps = new_apps
  129. db_table = "drop"
  130. def __str__(self):
  131. return self.when
  132. class UniqueTest(models.Model):
  133. year = models.IntegerField()
  134. slug = models.SlugField(unique=False)
  135. class Meta:
  136. apps = new_apps
  137. unique_together = ["year", "slug"]
  138. class Node(models.Model):
  139. node_id = models.AutoField(primary_key=True)
  140. parent = models.ForeignKey("self", models.CASCADE, null=True, blank=True)
  141. class Meta:
  142. apps = new_apps