models.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 AuthorWithIndexedNameAndBirthday(models.Model):
  41. name = models.CharField(max_length=255)
  42. birthday = models.DateField()
  43. class Meta:
  44. apps = new_apps
  45. index_together = [['name', 'birthday']]
  46. class AuthorWithUniqueNameAndBirthday(models.Model):
  47. name = models.CharField(max_length=255)
  48. birthday = models.DateField()
  49. class Meta:
  50. apps = new_apps
  51. unique_together = [['name', 'birthday']]
  52. class Book(models.Model):
  53. author = models.ForeignKey(Author, models.CASCADE)
  54. title = models.CharField(max_length=100, db_index=True)
  55. pub_date = models.DateTimeField()
  56. # tags = models.ManyToManyField("Tag", related_name="books")
  57. class Meta:
  58. apps = new_apps
  59. class BookWeak(models.Model):
  60. author = models.ForeignKey(Author, models.CASCADE, db_constraint=False)
  61. title = models.CharField(max_length=100, db_index=True)
  62. pub_date = models.DateTimeField()
  63. class Meta:
  64. apps = new_apps
  65. class BookWithLongName(models.Model):
  66. author_foreign_key_with_really_long_field_name = models.ForeignKey(
  67. AuthorWithEvenLongerName,
  68. models.CASCADE,
  69. )
  70. class Meta:
  71. apps = new_apps
  72. class BookWithO2O(models.Model):
  73. author = models.OneToOneField(Author, models.CASCADE)
  74. title = models.CharField(max_length=100, db_index=True)
  75. pub_date = models.DateTimeField()
  76. class Meta:
  77. apps = new_apps
  78. db_table = "schema_book"
  79. class BookWithSlug(models.Model):
  80. author = models.ForeignKey(Author, models.CASCADE)
  81. title = models.CharField(max_length=100, db_index=True)
  82. pub_date = models.DateTimeField()
  83. slug = models.CharField(max_length=20, unique=True)
  84. class Meta:
  85. apps = new_apps
  86. db_table = "schema_book"
  87. class BookWithoutAuthor(models.Model):
  88. title = models.CharField(max_length=100, db_index=True)
  89. pub_date = models.DateTimeField()
  90. class Meta:
  91. apps = new_apps
  92. db_table = "schema_book"
  93. class BookForeignObj(models.Model):
  94. title = models.CharField(max_length=100, db_index=True)
  95. author_id = models.IntegerField()
  96. class Meta:
  97. apps = new_apps
  98. class IntegerPK(models.Model):
  99. i = models.IntegerField(primary_key=True)
  100. j = models.IntegerField(unique=True)
  101. class Meta:
  102. apps = new_apps
  103. db_table = "INTEGERPK" # uppercase to ensure proper quoting
  104. class Note(models.Model):
  105. info = models.TextField()
  106. class Meta:
  107. apps = new_apps
  108. class NoteRename(models.Model):
  109. detail_info = models.TextField()
  110. class Meta:
  111. apps = new_apps
  112. db_table = "schema_note"
  113. class Tag(models.Model):
  114. title = models.CharField(max_length=255)
  115. slug = models.SlugField(unique=True)
  116. class Meta:
  117. apps = new_apps
  118. class TagIndexed(models.Model):
  119. title = models.CharField(max_length=255)
  120. slug = models.SlugField(unique=True)
  121. class Meta:
  122. apps = new_apps
  123. index_together = [["slug", "title"]]
  124. class TagM2MTest(models.Model):
  125. title = models.CharField(max_length=255)
  126. slug = models.SlugField(unique=True)
  127. class Meta:
  128. apps = new_apps
  129. class TagUniqueRename(models.Model):
  130. title = models.CharField(max_length=255)
  131. slug2 = models.SlugField(unique=True)
  132. class Meta:
  133. apps = new_apps
  134. db_table = "schema_tag"
  135. # Based on tests/reserved_names/models.py
  136. class Thing(models.Model):
  137. when = models.CharField(max_length=1, primary_key=True)
  138. class Meta:
  139. apps = new_apps
  140. db_table = 'drop'
  141. def __str__(self):
  142. return self.when
  143. class UniqueTest(models.Model):
  144. year = models.IntegerField()
  145. slug = models.SlugField(unique=False)
  146. class Meta:
  147. apps = new_apps
  148. unique_together = ["year", "slug"]
  149. class Node(models.Model):
  150. node_id = models.AutoField(primary_key=True)
  151. parent = models.ForeignKey('self', models.CASCADE, null=True, blank=True)
  152. class Meta:
  153. apps = new_apps