models.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. import json
  2. import tempfile
  3. import uuid
  4. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.core.files.storage import FileSystemStorage
  7. from django.core.serializers.json import DjangoJSONEncoder
  8. from django.db import connection, models
  9. from django.db.models import F, Value
  10. from django.db.models.fields.files import ImageFieldFile
  11. from django.db.models.functions import Lower
  12. from django.utils.functional import SimpleLazyObject
  13. from django.utils.translation import gettext_lazy as _
  14. from .storage import NoReadFileSystemStorage
  15. try:
  16. from PIL import Image
  17. except ImportError:
  18. Image = None
  19. # Set up a temp directory for file storage.
  20. temp_storage_dir = tempfile.mkdtemp()
  21. temp_storage = FileSystemStorage(temp_storage_dir)
  22. test_collation = SimpleLazyObject(
  23. lambda: connection.features.test_collations["virtual"]
  24. )
  25. class Foo(models.Model):
  26. a = models.CharField(max_length=10)
  27. d = models.DecimalField(max_digits=5, decimal_places=3)
  28. def get_foo():
  29. return Foo.objects.get(id=1).pk
  30. class Bar(models.Model):
  31. b = models.CharField(max_length=10)
  32. a = models.ForeignKey(Foo, models.CASCADE, default=get_foo, related_name="bars")
  33. class Whiz(models.Model):
  34. CHOICES = {
  35. "Group 1": {
  36. 1: "First",
  37. 2: "Second",
  38. },
  39. "Group 2": (
  40. (3, "Third"),
  41. (4, "Fourth"),
  42. ),
  43. 0: "Other",
  44. 5: _("translated"),
  45. }
  46. c = models.IntegerField(choices=CHOICES, null=True)
  47. class WhizDelayed(models.Model):
  48. c = models.IntegerField(choices=(), null=True)
  49. # Contrived way of adding choices later.
  50. WhizDelayed._meta.get_field("c").choices = Whiz.CHOICES
  51. class WhizIter(models.Model):
  52. c = models.IntegerField(choices=iter(Whiz.CHOICES.items()), null=True)
  53. class WhizIterEmpty(models.Model):
  54. c = models.CharField(choices=iter(()), blank=True, max_length=1)
  55. class Choiceful(models.Model):
  56. class Suit(models.IntegerChoices):
  57. DIAMOND = 1, "Diamond"
  58. SPADE = 2, "Spade"
  59. HEART = 3, "Heart"
  60. CLUB = 4, "Club"
  61. def get_choices():
  62. return [(i, str(i)) for i in range(3)]
  63. no_choices = models.IntegerField(null=True)
  64. empty_choices = models.IntegerField(choices=(), null=True)
  65. with_choices = models.IntegerField(choices=[(1, "A")], null=True)
  66. with_choices_dict = models.IntegerField(choices={1: "A"}, null=True)
  67. with_choices_nested_dict = models.IntegerField(
  68. choices={"Thing": {1: "A"}}, null=True
  69. )
  70. empty_choices_bool = models.BooleanField(choices=())
  71. empty_choices_text = models.TextField(choices=())
  72. choices_from_enum = models.IntegerField(choices=Suit)
  73. choices_from_iterator = models.IntegerField(choices=((i, str(i)) for i in range(3)))
  74. choices_from_callable = models.IntegerField(choices=get_choices)
  75. class BigD(models.Model):
  76. d = models.DecimalField(max_digits=32, decimal_places=30)
  77. class FloatModel(models.Model):
  78. size = models.FloatField()
  79. class BigS(models.Model):
  80. s = models.SlugField(max_length=255)
  81. class UnicodeSlugField(models.Model):
  82. s = models.SlugField(max_length=255, allow_unicode=True)
  83. class AutoModel(models.Model):
  84. value = models.AutoField(primary_key=True)
  85. class BigAutoModel(models.Model):
  86. value = models.BigAutoField(primary_key=True)
  87. class SmallAutoModel(models.Model):
  88. value = models.SmallAutoField(primary_key=True)
  89. class SmallIntegerModel(models.Model):
  90. value = models.SmallIntegerField()
  91. class IntegerModel(models.Model):
  92. value = models.IntegerField()
  93. class BigIntegerModel(models.Model):
  94. value = models.BigIntegerField()
  95. null_value = models.BigIntegerField(null=True, blank=True)
  96. class PositiveBigIntegerModel(models.Model):
  97. value = models.PositiveBigIntegerField()
  98. class PositiveSmallIntegerModel(models.Model):
  99. value = models.PositiveSmallIntegerField()
  100. class PositiveIntegerModel(models.Model):
  101. value = models.PositiveIntegerField()
  102. class Post(models.Model):
  103. title = models.CharField(max_length=100)
  104. body = models.TextField()
  105. class NullBooleanModel(models.Model):
  106. nbfield = models.BooleanField(null=True, blank=True)
  107. class BooleanModel(models.Model):
  108. bfield = models.BooleanField()
  109. class DateTimeModel(models.Model):
  110. d = models.DateField()
  111. dt = models.DateTimeField()
  112. t = models.TimeField()
  113. class DurationModel(models.Model):
  114. field = models.DurationField()
  115. class NullDurationModel(models.Model):
  116. field = models.DurationField(null=True)
  117. class PrimaryKeyCharModel(models.Model):
  118. string = models.CharField(max_length=10, primary_key=True)
  119. class FksToBooleans(models.Model):
  120. """Model with FKs to models with {Null,}BooleanField's, #15040"""
  121. bf = models.ForeignKey(BooleanModel, models.CASCADE)
  122. nbf = models.ForeignKey(NullBooleanModel, models.CASCADE)
  123. class FkToChar(models.Model):
  124. """Model with FK to a model with a CharField primary key, #19299"""
  125. out = models.ForeignKey(PrimaryKeyCharModel, models.CASCADE)
  126. class RenamedField(models.Model):
  127. modelname = models.IntegerField(name="fieldname", choices=((1, "One"),))
  128. class VerboseNameField(models.Model):
  129. id = models.AutoField("verbose pk", primary_key=True)
  130. field1 = models.BigIntegerField("verbose field1")
  131. field2 = models.BooleanField("verbose field2", default=False)
  132. field3 = models.CharField("verbose field3", max_length=10)
  133. field4 = models.DateField("verbose field4")
  134. field5 = models.DateTimeField("verbose field5")
  135. field6 = models.DecimalField("verbose field6", max_digits=6, decimal_places=1)
  136. field7 = models.EmailField("verbose field7")
  137. field8 = models.FileField(
  138. "verbose field8", storage=temp_storage, upload_to="unused"
  139. )
  140. field9 = models.FilePathField("verbose field9")
  141. field10 = models.FloatField("verbose field10")
  142. # Don't want to depend on Pillow in this test
  143. # field_image = models.ImageField("verbose field")
  144. field11 = models.IntegerField("verbose field11")
  145. field12 = models.GenericIPAddressField("verbose field12", protocol="ipv4")
  146. field13 = models.PositiveIntegerField("verbose field13")
  147. field14 = models.PositiveSmallIntegerField("verbose field14")
  148. field15 = models.SlugField("verbose field15")
  149. field16 = models.SmallIntegerField("verbose field16")
  150. field17 = models.TextField("verbose field17")
  151. field18 = models.TimeField("verbose field18")
  152. field19 = models.URLField("verbose field19")
  153. field20 = models.UUIDField("verbose field20")
  154. field21 = models.DurationField("verbose field21")
  155. class GenericIPAddress(models.Model):
  156. ip = models.GenericIPAddressField(null=True, protocol="ipv4")
  157. ###############################################################################
  158. # These models aren't used in any test, just here to ensure they validate
  159. # successfully.
  160. # See ticket #16570.
  161. class DecimalLessThanOne(models.Model):
  162. d = models.DecimalField(max_digits=3, decimal_places=3)
  163. # See ticket #18389.
  164. class FieldClassAttributeModel(models.Model):
  165. field_class = models.CharField
  166. ###############################################################################
  167. class DataModel(models.Model):
  168. short_data = models.BinaryField(max_length=10, default=b"\x08")
  169. data = models.BinaryField()
  170. ###############################################################################
  171. # FileField
  172. class Document(models.Model):
  173. myfile = models.FileField(storage=temp_storage, upload_to="unused", unique=True)
  174. ###############################################################################
  175. # ImageField
  176. # If Pillow available, do these tests.
  177. if Image:
  178. class TestImageFieldFile(ImageFieldFile):
  179. """
  180. Custom Field File class that records whether or not the underlying file
  181. was opened.
  182. """
  183. def __init__(self, *args, **kwargs):
  184. self.was_opened = False
  185. super().__init__(*args, **kwargs)
  186. def open(self):
  187. self.was_opened = True
  188. super().open()
  189. class TestImageField(models.ImageField):
  190. attr_class = TestImageFieldFile
  191. class Person(models.Model):
  192. """
  193. Model that defines an ImageField with no dimension fields.
  194. """
  195. name = models.CharField(max_length=50)
  196. mugshot = TestImageField(storage=temp_storage, upload_to="tests")
  197. class AbstractPersonWithHeight(models.Model):
  198. """
  199. Abstract model that defines an ImageField with only one dimension field
  200. to make sure the dimension update is correctly run on concrete subclass
  201. instance post-initialization.
  202. """
  203. mugshot = TestImageField(
  204. storage=temp_storage, upload_to="tests", height_field="mugshot_height"
  205. )
  206. mugshot_height = models.PositiveSmallIntegerField()
  207. class Meta:
  208. abstract = True
  209. class PersonWithHeight(AbstractPersonWithHeight):
  210. """
  211. Concrete model that subclass an abstract one with only on dimension
  212. field.
  213. """
  214. name = models.CharField(max_length=50)
  215. class PersonWithHeightAndWidth(models.Model):
  216. """
  217. Model that defines height and width fields after the ImageField.
  218. """
  219. name = models.CharField(max_length=50)
  220. mugshot = TestImageField(
  221. storage=temp_storage,
  222. upload_to="tests",
  223. height_field="mugshot_height",
  224. width_field="mugshot_width",
  225. )
  226. mugshot_height = models.PositiveSmallIntegerField()
  227. mugshot_width = models.PositiveSmallIntegerField()
  228. class PersonDimensionsFirst(models.Model):
  229. """
  230. Model that defines height and width fields before the ImageField.
  231. """
  232. name = models.CharField(max_length=50)
  233. mugshot_height = models.PositiveSmallIntegerField()
  234. mugshot_width = models.PositiveSmallIntegerField()
  235. mugshot = TestImageField(
  236. storage=temp_storage,
  237. upload_to="tests",
  238. height_field="mugshot_height",
  239. width_field="mugshot_width",
  240. )
  241. class PersonTwoImages(models.Model):
  242. """
  243. Model that:
  244. * Defines two ImageFields
  245. * Defines the height/width fields before the ImageFields
  246. * Has a nullable ImageField
  247. """
  248. name = models.CharField(max_length=50)
  249. mugshot_height = models.PositiveSmallIntegerField()
  250. mugshot_width = models.PositiveSmallIntegerField()
  251. mugshot = TestImageField(
  252. storage=temp_storage,
  253. upload_to="tests",
  254. height_field="mugshot_height",
  255. width_field="mugshot_width",
  256. )
  257. headshot_height = models.PositiveSmallIntegerField(blank=True, null=True)
  258. headshot_width = models.PositiveSmallIntegerField(blank=True, null=True)
  259. headshot = TestImageField(
  260. blank=True,
  261. null=True,
  262. storage=temp_storage,
  263. upload_to="tests",
  264. height_field="headshot_height",
  265. width_field="headshot_width",
  266. )
  267. class PersonNoReadImage(models.Model):
  268. """
  269. Model that defines an ImageField with a storage backend that does not
  270. support reading.
  271. """
  272. mugshot = models.ImageField(
  273. upload_to="tests",
  274. storage=NoReadFileSystemStorage(temp_storage_dir),
  275. width_field="mugshot_width",
  276. height_field="mugshot_height",
  277. )
  278. mugshot_width = models.IntegerField()
  279. mugshot_height = models.IntegerField()
  280. class CustomJSONDecoder(json.JSONDecoder):
  281. def __init__(self, object_hook=None, *args, **kwargs):
  282. return super().__init__(object_hook=self.as_uuid, *args, **kwargs)
  283. def as_uuid(self, dct):
  284. if "uuid" in dct:
  285. dct["uuid"] = uuid.UUID(dct["uuid"])
  286. return dct
  287. class JSONModel(models.Model):
  288. value = models.JSONField()
  289. class Meta:
  290. required_db_features = {"supports_json_field"}
  291. class NullableJSONModel(models.Model):
  292. value = models.JSONField(blank=True, null=True)
  293. value_custom = models.JSONField(
  294. encoder=DjangoJSONEncoder,
  295. decoder=CustomJSONDecoder,
  296. null=True,
  297. )
  298. class Meta:
  299. required_db_features = {"supports_json_field"}
  300. class RelatedJSONModel(models.Model):
  301. value = models.JSONField()
  302. json_model = models.ForeignKey(NullableJSONModel, models.CASCADE)
  303. class Meta:
  304. required_db_features = {"supports_json_field"}
  305. class CustomSerializationJSONModel(models.Model):
  306. class StringifiedJSONField(models.JSONField):
  307. def get_prep_value(self, value):
  308. return json.dumps(value, cls=self.encoder)
  309. json_field = StringifiedJSONField()
  310. class Meta:
  311. required_db_features = {
  312. "supports_json_field",
  313. "supports_primitives_in_json_field",
  314. }
  315. class AllFieldsModel(models.Model):
  316. big_integer = models.BigIntegerField()
  317. binary = models.BinaryField()
  318. boolean = models.BooleanField(default=False)
  319. char = models.CharField(max_length=10)
  320. date = models.DateField()
  321. datetime = models.DateTimeField()
  322. decimal = models.DecimalField(decimal_places=2, max_digits=2)
  323. duration = models.DurationField()
  324. email = models.EmailField()
  325. file_path = models.FilePathField()
  326. floatf = models.FloatField()
  327. integer = models.IntegerField()
  328. generic_ip = models.GenericIPAddressField()
  329. positive_integer = models.PositiveIntegerField()
  330. positive_small_integer = models.PositiveSmallIntegerField()
  331. slug = models.SlugField()
  332. small_integer = models.SmallIntegerField()
  333. text = models.TextField()
  334. time = models.TimeField()
  335. url = models.URLField()
  336. uuid = models.UUIDField()
  337. fo = models.ForeignObject(
  338. "self",
  339. on_delete=models.CASCADE,
  340. from_fields=["positive_integer"],
  341. to_fields=["id"],
  342. related_name="reverse",
  343. )
  344. fk = models.ForeignKey("self", models.CASCADE, related_name="reverse2")
  345. m2m = models.ManyToManyField("self")
  346. oto = models.OneToOneField("self", models.CASCADE)
  347. object_id = models.PositiveIntegerField()
  348. content_type = models.ForeignKey(ContentType, models.CASCADE)
  349. gfk = GenericForeignKey()
  350. gr = GenericRelation(DataModel)
  351. class ManyToMany(models.Model):
  352. m2m = models.ManyToManyField("self")
  353. ###############################################################################
  354. class UUIDModel(models.Model):
  355. field = models.UUIDField()
  356. class NullableUUIDModel(models.Model):
  357. field = models.UUIDField(blank=True, null=True)
  358. class PrimaryKeyUUIDModel(models.Model):
  359. id = models.UUIDField(primary_key=True, default=uuid.uuid4)
  360. class RelatedToUUIDModel(models.Model):
  361. uuid_fk = models.ForeignKey("PrimaryKeyUUIDModel", models.CASCADE)
  362. class UUIDChild(PrimaryKeyUUIDModel):
  363. pass
  364. class UUIDGrandchild(UUIDChild):
  365. pass
  366. class GeneratedModelFieldWithConverters(models.Model):
  367. field = models.UUIDField()
  368. field_copy = models.GeneratedField(
  369. expression=F("field"),
  370. output_field=models.UUIDField(),
  371. db_persist=True,
  372. )
  373. class Meta:
  374. required_db_features = {"supports_stored_generated_columns"}
  375. class GeneratedModel(models.Model):
  376. a = models.IntegerField()
  377. b = models.IntegerField()
  378. field = models.GeneratedField(
  379. expression=F("a") + F("b"),
  380. output_field=models.IntegerField(),
  381. db_persist=True,
  382. )
  383. fk = models.ForeignKey(Foo, on_delete=models.CASCADE, null=True, blank=True)
  384. class Meta:
  385. required_db_features = {"supports_stored_generated_columns"}
  386. class GeneratedModelNonAutoPk(models.Model):
  387. id = models.IntegerField(primary_key=True)
  388. a = models.IntegerField()
  389. b = models.GeneratedField(
  390. expression=F("a"),
  391. output_field=models.IntegerField(),
  392. db_persist=True,
  393. )
  394. class Meta:
  395. required_db_features = {"supports_stored_generated_columns"}
  396. class GeneratedModelVirtual(models.Model):
  397. a = models.IntegerField()
  398. b = models.IntegerField()
  399. field = models.GeneratedField(
  400. expression=F("a") + F("b"),
  401. output_field=models.IntegerField(),
  402. db_persist=False,
  403. )
  404. fk = models.ForeignKey(Foo, on_delete=models.CASCADE, null=True, blank=True)
  405. class Meta:
  406. required_db_features = {"supports_virtual_generated_columns"}
  407. class GeneratedModelParams(models.Model):
  408. field = models.GeneratedField(
  409. expression=Value("Constant", output_field=models.CharField(max_length=10)),
  410. output_field=models.CharField(max_length=10),
  411. db_persist=True,
  412. )
  413. class Meta:
  414. required_db_features = {"supports_stored_generated_columns"}
  415. class GeneratedModelParamsVirtual(models.Model):
  416. field = models.GeneratedField(
  417. expression=Value("Constant", output_field=models.CharField(max_length=10)),
  418. output_field=models.CharField(max_length=10),
  419. db_persist=False,
  420. )
  421. class Meta:
  422. required_db_features = {"supports_virtual_generated_columns"}
  423. class GeneratedModelOutputFieldDbCollation(models.Model):
  424. name = models.CharField(max_length=10)
  425. lower_name = models.GeneratedField(
  426. expression=Lower("name"),
  427. output_field=models.CharField(db_collation=test_collation, max_length=11),
  428. db_persist=True,
  429. )
  430. class Meta:
  431. required_db_features = {"supports_stored_generated_columns"}
  432. class GeneratedModelOutputFieldDbCollationVirtual(models.Model):
  433. name = models.CharField(max_length=10)
  434. lower_name = models.GeneratedField(
  435. expression=Lower("name"),
  436. db_persist=False,
  437. output_field=models.CharField(db_collation=test_collation, max_length=11),
  438. )
  439. class Meta:
  440. required_db_features = {"supports_virtual_generated_columns"}
  441. class GeneratedModelNull(models.Model):
  442. name = models.CharField(max_length=10, null=True)
  443. lower_name = models.GeneratedField(
  444. expression=Lower("name"),
  445. output_field=models.CharField(max_length=10),
  446. db_persist=True,
  447. null=True,
  448. )
  449. class Meta:
  450. required_db_features = {"supports_stored_generated_columns"}
  451. class GeneratedModelNullVirtual(models.Model):
  452. name = models.CharField(max_length=10, null=True)
  453. lower_name = models.GeneratedField(
  454. expression=Lower("name"),
  455. output_field=models.CharField(max_length=10),
  456. db_persist=False,
  457. null=True,
  458. )
  459. class Meta:
  460. required_db_features = {"supports_virtual_generated_columns"}
  461. class GeneratedModelBase(models.Model):
  462. a = models.IntegerField()
  463. a_squared = models.GeneratedField(
  464. expression=F("a") * F("a"),
  465. output_field=models.IntegerField(),
  466. db_persist=True,
  467. )
  468. class Meta:
  469. abstract = True
  470. class GeneratedModelVirtualBase(models.Model):
  471. a = models.IntegerField()
  472. a_squared = models.GeneratedField(
  473. expression=F("a") * F("a"),
  474. output_field=models.IntegerField(),
  475. db_persist=False,
  476. )
  477. class Meta:
  478. abstract = True
  479. class GeneratedModelCheckConstraint(GeneratedModelBase):
  480. class Meta:
  481. required_db_features = {
  482. "supports_stored_generated_columns",
  483. "supports_table_check_constraints",
  484. }
  485. constraints = [
  486. models.CheckConstraint(
  487. condition=models.Q(a__gt=0),
  488. name="Generated model check constraint a > 0",
  489. )
  490. ]
  491. class GeneratedModelCheckConstraintVirtual(GeneratedModelVirtualBase):
  492. class Meta:
  493. required_db_features = {
  494. "supports_virtual_generated_columns",
  495. "supports_table_check_constraints",
  496. }
  497. constraints = [
  498. models.CheckConstraint(
  499. condition=models.Q(a__gt=0),
  500. name="Generated model check constraint virtual a > 0",
  501. )
  502. ]
  503. class GeneratedModelUniqueConstraint(GeneratedModelBase):
  504. class Meta:
  505. required_db_features = {
  506. "supports_stored_generated_columns",
  507. "supports_expression_indexes",
  508. }
  509. constraints = [
  510. models.UniqueConstraint(F("a"), name="Generated model unique constraint a"),
  511. ]
  512. class GeneratedModelUniqueConstraintVirtual(GeneratedModelVirtualBase):
  513. class Meta:
  514. required_db_features = {
  515. "supports_virtual_generated_columns",
  516. "supports_expression_indexes",
  517. }
  518. constraints = [
  519. models.UniqueConstraint(
  520. F("a"), name="Generated model unique constraint virtual a"
  521. ),
  522. ]