2
0

models.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import os
  2. import tempfile
  3. import uuid
  4. try:
  5. from PIL import Image
  6. except ImportError:
  7. Image = None
  8. from django.core.files.storage import FileSystemStorage
  9. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  10. from django.contrib.contenttypes.models import ContentType
  11. from django.db.models.fields.related import (
  12. ForeignObject, ForeignKey, ManyToManyField, OneToOneField,
  13. )
  14. from django.db import models
  15. from django.db.models.fields.files import ImageFieldFile, ImageField
  16. from django.utils import six
  17. class Foo(models.Model):
  18. a = models.CharField(max_length=10)
  19. d = models.DecimalField(max_digits=5, decimal_places=3)
  20. def get_foo():
  21. return Foo.objects.get(id=1)
  22. class Bar(models.Model):
  23. b = models.CharField(max_length=10)
  24. a = models.ForeignKey(Foo, default=get_foo, related_name=b'bars')
  25. class Whiz(models.Model):
  26. CHOICES = (
  27. ('Group 1', (
  28. (1, 'First'),
  29. (2, 'Second'),
  30. )
  31. ),
  32. ('Group 2', (
  33. (3, 'Third'),
  34. (4, 'Fourth'),
  35. )
  36. ),
  37. (0, 'Other'),
  38. )
  39. c = models.IntegerField(choices=CHOICES, null=True)
  40. class Counter(six.Iterator):
  41. def __init__(self):
  42. self.n = 1
  43. def __iter__(self):
  44. return self
  45. def __next__(self):
  46. if self.n > 5:
  47. raise StopIteration
  48. else:
  49. self.n += 1
  50. return (self.n, 'val-' + str(self.n))
  51. class WhizIter(models.Model):
  52. c = models.IntegerField(choices=Counter(), null=True)
  53. class WhizIterEmpty(models.Model):
  54. c = models.CharField(choices=(x for x in []), blank=True, max_length=1)
  55. class BigD(models.Model):
  56. d = models.DecimalField(max_digits=38, decimal_places=30)
  57. class FloatModel(models.Model):
  58. size = models.FloatField()
  59. class BigS(models.Model):
  60. s = models.SlugField(max_length=255)
  61. class SmallIntegerModel(models.Model):
  62. value = models.SmallIntegerField()
  63. class IntegerModel(models.Model):
  64. value = models.IntegerField()
  65. class BigIntegerModel(models.Model):
  66. value = models.BigIntegerField()
  67. null_value = models.BigIntegerField(null=True, blank=True)
  68. class PositiveSmallIntegerModel(models.Model):
  69. value = models.PositiveSmallIntegerField()
  70. class PositiveIntegerModel(models.Model):
  71. value = models.PositiveIntegerField()
  72. class Post(models.Model):
  73. title = models.CharField(max_length=100)
  74. body = models.TextField()
  75. class NullBooleanModel(models.Model):
  76. nbfield = models.NullBooleanField()
  77. class BooleanModel(models.Model):
  78. bfield = models.BooleanField(default=None)
  79. string = models.CharField(max_length=10, default='abc')
  80. class DateTimeModel(models.Model):
  81. d = models.DateField()
  82. dt = models.DateTimeField()
  83. t = models.TimeField()
  84. class DurationModel(models.Model):
  85. field = models.DurationField()
  86. class PrimaryKeyCharModel(models.Model):
  87. string = models.CharField(max_length=10, primary_key=True)
  88. class FksToBooleans(models.Model):
  89. """Model with FKs to models with {Null,}BooleanField's, #15040"""
  90. bf = models.ForeignKey(BooleanModel)
  91. nbf = models.ForeignKey(NullBooleanModel)
  92. class FkToChar(models.Model):
  93. """Model with FK to a model with a CharField primary key, #19299"""
  94. out = models.ForeignKey(PrimaryKeyCharModel)
  95. class RenamedField(models.Model):
  96. modelname = models.IntegerField(name="fieldname", choices=((1, 'One'),))
  97. class VerboseNameField(models.Model):
  98. id = models.AutoField("verbose pk", primary_key=True)
  99. field1 = models.BigIntegerField("verbose field1")
  100. field2 = models.BooleanField("verbose field2", default=False)
  101. field3 = models.CharField("verbose field3", max_length=10)
  102. field4 = models.CommaSeparatedIntegerField("verbose field4", max_length=99)
  103. field5 = models.DateField("verbose field5")
  104. field6 = models.DateTimeField("verbose field6")
  105. field7 = models.DecimalField("verbose field7", max_digits=6, decimal_places=1)
  106. field8 = models.EmailField("verbose field8")
  107. field9 = models.FileField("verbose field9", upload_to="unused")
  108. field10 = models.FilePathField("verbose field10")
  109. field11 = models.FloatField("verbose field11")
  110. # Don't want to depend on Pillow in this test
  111. #field_image = models.ImageField("verbose field")
  112. field12 = models.IntegerField("verbose field12")
  113. field13 = models.IPAddressField("verbose field13")
  114. field14 = models.GenericIPAddressField("verbose field14", protocol="ipv4")
  115. field15 = models.NullBooleanField("verbose field15")
  116. field16 = models.PositiveIntegerField("verbose field16")
  117. field17 = models.PositiveSmallIntegerField("verbose field17")
  118. field18 = models.SlugField("verbose field18")
  119. field19 = models.SmallIntegerField("verbose field19")
  120. field20 = models.TextField("verbose field20")
  121. field21 = models.TimeField("verbose field21")
  122. field22 = models.URLField("verbose field22")
  123. ###############################################################################
  124. # These models aren't used in any test, just here to ensure they validate
  125. # successfully.
  126. # See ticket #16570.
  127. class DecimalLessThanOne(models.Model):
  128. d = models.DecimalField(max_digits=3, decimal_places=3)
  129. # See ticket #18389.
  130. class FieldClassAttributeModel(models.Model):
  131. field_class = models.CharField
  132. ###############################################################################
  133. class DataModel(models.Model):
  134. short_data = models.BinaryField(max_length=10, default=b'\x08')
  135. data = models.BinaryField()
  136. ###############################################################################
  137. # FileField
  138. class Document(models.Model):
  139. myfile = models.FileField(upload_to='unused')
  140. ###############################################################################
  141. # ImageField
  142. # If Pillow available, do these tests.
  143. if Image:
  144. class TestImageFieldFile(ImageFieldFile):
  145. """
  146. Custom Field File class that records whether or not the underlying file
  147. was opened.
  148. """
  149. def __init__(self, *args, **kwargs):
  150. self.was_opened = False
  151. super(TestImageFieldFile, self).__init__(*args, **kwargs)
  152. def open(self):
  153. self.was_opened = True
  154. super(TestImageFieldFile, self).open()
  155. class TestImageField(ImageField):
  156. attr_class = TestImageFieldFile
  157. # Set up a temp directory for file storage.
  158. temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
  159. temp_storage = FileSystemStorage(temp_storage_dir)
  160. temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
  161. class Person(models.Model):
  162. """
  163. Model that defines an ImageField with no dimension fields.
  164. """
  165. name = models.CharField(max_length=50)
  166. mugshot = TestImageField(storage=temp_storage, upload_to='tests')
  167. class AbsctractPersonWithHeight(models.Model):
  168. """
  169. Abstract model that defines an ImageField with only one dimension field
  170. to make sure the dimension update is correctly run on concrete subclass
  171. instance post-initialization.
  172. """
  173. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  174. height_field='mugshot_height')
  175. mugshot_height = models.PositiveSmallIntegerField()
  176. class Meta:
  177. abstract = True
  178. class PersonWithHeight(AbsctractPersonWithHeight):
  179. """
  180. Concrete model that subclass an abctract one with only on dimension
  181. field.
  182. """
  183. name = models.CharField(max_length=50)
  184. class PersonWithHeightAndWidth(models.Model):
  185. """
  186. Model that defines height and width fields after the ImageField.
  187. """
  188. name = models.CharField(max_length=50)
  189. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  190. height_field='mugshot_height',
  191. width_field='mugshot_width')
  192. mugshot_height = models.PositiveSmallIntegerField()
  193. mugshot_width = models.PositiveSmallIntegerField()
  194. class PersonDimensionsFirst(models.Model):
  195. """
  196. Model that defines height and width fields before the ImageField.
  197. """
  198. name = models.CharField(max_length=50)
  199. mugshot_height = models.PositiveSmallIntegerField()
  200. mugshot_width = models.PositiveSmallIntegerField()
  201. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  202. height_field='mugshot_height',
  203. width_field='mugshot_width')
  204. class PersonTwoImages(models.Model):
  205. """
  206. Model that:
  207. * Defines two ImageFields
  208. * Defines the height/width fields before the ImageFields
  209. * Has a nullalble ImageField
  210. """
  211. name = models.CharField(max_length=50)
  212. mugshot_height = models.PositiveSmallIntegerField()
  213. mugshot_width = models.PositiveSmallIntegerField()
  214. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  215. height_field='mugshot_height',
  216. width_field='mugshot_width')
  217. headshot_height = models.PositiveSmallIntegerField(
  218. blank=True, null=True)
  219. headshot_width = models.PositiveSmallIntegerField(
  220. blank=True, null=True)
  221. headshot = TestImageField(blank=True, null=True,
  222. storage=temp_storage, upload_to='tests',
  223. height_field='headshot_height',
  224. width_field='headshot_width')
  225. class AllFieldsModel(models.Model):
  226. big_integer = models.BigIntegerField()
  227. binary = models.BinaryField()
  228. boolean = models.BooleanField(default=False)
  229. char = models.CharField(max_length=10)
  230. csv = models.CommaSeparatedIntegerField(max_length=10)
  231. date = models.DateField()
  232. datetime = models.DateTimeField()
  233. decimal = models.DecimalField(decimal_places=2, max_digits=2)
  234. duration = models.DurationField()
  235. email = models.EmailField()
  236. file_path = models.FilePathField()
  237. floatf = models.FloatField()
  238. integer = models.IntegerField()
  239. ip_address = models.IPAddressField()
  240. generic_ip = models.GenericIPAddressField()
  241. null_boolean = models.NullBooleanField()
  242. positive_integer = models.PositiveIntegerField()
  243. positive_small_integer = models.PositiveSmallIntegerField()
  244. slug = models.SlugField()
  245. small_integer = models.SmallIntegerField()
  246. text = models.TextField()
  247. time = models.TimeField()
  248. url = models.URLField()
  249. uuid = models.UUIDField()
  250. fo = ForeignObject(
  251. 'self',
  252. from_fields=['abstract_non_concrete_id'],
  253. to_fields=['id'],
  254. related_name='reverse'
  255. )
  256. fk = ForeignKey(
  257. 'self',
  258. related_name='reverse2'
  259. )
  260. m2m = ManyToManyField('self')
  261. oto = OneToOneField('self')
  262. object_id = models.PositiveIntegerField()
  263. content_type = models.ForeignKey(ContentType)
  264. gfk = GenericForeignKey()
  265. gr = GenericRelation(DataModel)
  266. ###############################################################################
  267. class UUIDModel(models.Model):
  268. field = models.UUIDField()
  269. class NullableUUIDModel(models.Model):
  270. field = models.UUIDField(blank=True, null=True)
  271. class PrimaryKeyUUIDModel(models.Model):
  272. id = models.UUIDField(primary_key=True, default=uuid.uuid4)