models.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import os
  2. import tempfile
  3. import warnings
  4. try:
  5. from PIL import Image
  6. except ImportError:
  7. Image = None
  8. from django.core.files.storage import FileSystemStorage
  9. from django.db import models
  10. from django.db.models.fields.files import ImageFieldFile, ImageField
  11. class Foo(models.Model):
  12. a = models.CharField(max_length=10)
  13. d = models.DecimalField(max_digits=5, decimal_places=3)
  14. def get_foo():
  15. return Foo.objects.get(id=1)
  16. class Bar(models.Model):
  17. b = models.CharField(max_length=10)
  18. a = models.ForeignKey(Foo, default=get_foo)
  19. class Whiz(models.Model):
  20. CHOICES = (
  21. ('Group 1', (
  22. (1, 'First'),
  23. (2, 'Second'),
  24. )
  25. ),
  26. ('Group 2', (
  27. (3, 'Third'),
  28. (4, 'Fourth'),
  29. )
  30. ),
  31. (0, 'Other'),
  32. )
  33. c = models.IntegerField(choices=CHOICES, null=True)
  34. class BigD(models.Model):
  35. d = models.DecimalField(max_digits=38, decimal_places=30)
  36. class FloatModel(models.Model):
  37. size = models.FloatField()
  38. class BigS(models.Model):
  39. s = models.SlugField(max_length=255)
  40. class BigInt(models.Model):
  41. value = models.BigIntegerField()
  42. null_value = models.BigIntegerField(null=True, blank=True)
  43. class Post(models.Model):
  44. title = models.CharField(max_length=100)
  45. body = models.TextField()
  46. class NullBooleanModel(models.Model):
  47. nbfield = models.NullBooleanField()
  48. class BooleanModel(models.Model):
  49. bfield = models.BooleanField(default=None)
  50. string = models.CharField(max_length=10, default='abc')
  51. class DateTimeModel(models.Model):
  52. d = models.DateField()
  53. dt = models.DateTimeField()
  54. t = models.TimeField()
  55. class PrimaryKeyCharModel(models.Model):
  56. string = models.CharField(max_length=10, primary_key=True)
  57. class FksToBooleans(models.Model):
  58. """Model wih FKs to models with {Null,}BooleanField's, #15040"""
  59. bf = models.ForeignKey(BooleanModel)
  60. nbf = models.ForeignKey(NullBooleanModel)
  61. class FkToChar(models.Model):
  62. """Model with FK to a model with a CharField primary key, #19299"""
  63. out = models.ForeignKey(PrimaryKeyCharModel)
  64. class RenamedField(models.Model):
  65. modelname = models.IntegerField(name="fieldname", choices=((1, 'One'),))
  66. class VerboseNameField(models.Model):
  67. id = models.AutoField("verbose pk", primary_key=True)
  68. field1 = models.BigIntegerField("verbose field1")
  69. field2 = models.BooleanField("verbose field2", default=False)
  70. field3 = models.CharField("verbose field3", max_length=10)
  71. field4 = models.CommaSeparatedIntegerField("verbose field4", max_length=99)
  72. field5 = models.DateField("verbose field5")
  73. field6 = models.DateTimeField("verbose field6")
  74. field7 = models.DecimalField("verbose field7", max_digits=6, decimal_places=1)
  75. field8 = models.EmailField("verbose field8")
  76. field9 = models.FileField("verbose field9", upload_to="unused")
  77. field10 = models.FilePathField("verbose field10")
  78. field11 = models.FloatField("verbose field11")
  79. # Don't want to depend on Pillow in this test
  80. #field_image = models.ImageField("verbose field")
  81. field12 = models.IntegerField("verbose field12")
  82. with warnings.catch_warnings(record=True) as w:
  83. warnings.simplefilter("always")
  84. field13 = models.IPAddressField("verbose field13")
  85. field14 = models.GenericIPAddressField("verbose field14", protocol="ipv4")
  86. field15 = models.NullBooleanField("verbose field15")
  87. field16 = models.PositiveIntegerField("verbose field16")
  88. field17 = models.PositiveSmallIntegerField("verbose field17")
  89. field18 = models.SlugField("verbose field18")
  90. field19 = models.SmallIntegerField("verbose field19")
  91. field20 = models.TextField("verbose field20")
  92. field21 = models.TimeField("verbose field21")
  93. field22 = models.URLField("verbose field22")
  94. # This model isn't used in any test, just here to ensure it validates successfully.
  95. # See ticket #16570.
  96. class DecimalLessThanOne(models.Model):
  97. d = models.DecimalField(max_digits=3, decimal_places=3)
  98. class DataModel(models.Model):
  99. short_data = models.BinaryField(max_length=10, default=b'\x08')
  100. data = models.BinaryField()
  101. ###############################################################################
  102. # FileField
  103. class Document(models.Model):
  104. myfile = models.FileField(upload_to='unused')
  105. ###############################################################################
  106. # ImageField
  107. # If Pillow available, do these tests.
  108. if Image:
  109. class TestImageFieldFile(ImageFieldFile):
  110. """
  111. Custom Field File class that records whether or not the underlying file
  112. was opened.
  113. """
  114. def __init__(self, *args, **kwargs):
  115. self.was_opened = False
  116. super(TestImageFieldFile, self).__init__(*args, **kwargs)
  117. def open(self):
  118. self.was_opened = True
  119. super(TestImageFieldFile, self).open()
  120. class TestImageField(ImageField):
  121. attr_class = TestImageFieldFile
  122. # Set up a temp directory for file storage.
  123. temp_storage_dir = tempfile.mkdtemp()
  124. temp_storage = FileSystemStorage(temp_storage_dir)
  125. temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
  126. class Person(models.Model):
  127. """
  128. Model that defines an ImageField with no dimension fields.
  129. """
  130. name = models.CharField(max_length=50)
  131. mugshot = TestImageField(storage=temp_storage, upload_to='tests')
  132. class AbsctractPersonWithHeight(models.Model):
  133. """
  134. Abstract model that defines an ImageField with only one dimension field
  135. to make sure the dimension update is correctly run on concrete subclass
  136. instance post-initialization.
  137. """
  138. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  139. height_field='mugshot_height')
  140. mugshot_height = models.PositiveSmallIntegerField()
  141. class Meta:
  142. abstract = True
  143. class PersonWithHeight(AbsctractPersonWithHeight):
  144. """
  145. Concrete model that subclass an abctract one with only on dimension
  146. field.
  147. """
  148. name = models.CharField(max_length=50)
  149. class PersonWithHeightAndWidth(models.Model):
  150. """
  151. Model that defines height and width fields after the ImageField.
  152. """
  153. name = models.CharField(max_length=50)
  154. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  155. height_field='mugshot_height',
  156. width_field='mugshot_width')
  157. mugshot_height = models.PositiveSmallIntegerField()
  158. mugshot_width = models.PositiveSmallIntegerField()
  159. class PersonDimensionsFirst(models.Model):
  160. """
  161. Model that defines height and width fields before the ImageField.
  162. """
  163. name = models.CharField(max_length=50)
  164. mugshot_height = models.PositiveSmallIntegerField()
  165. mugshot_width = models.PositiveSmallIntegerField()
  166. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  167. height_field='mugshot_height',
  168. width_field='mugshot_width')
  169. class PersonTwoImages(models.Model):
  170. """
  171. Model that:
  172. * Defines two ImageFields
  173. * Defines the height/width fields before the ImageFields
  174. * Has a nullalble ImageField
  175. """
  176. name = models.CharField(max_length=50)
  177. mugshot_height = models.PositiveSmallIntegerField()
  178. mugshot_width = models.PositiveSmallIntegerField()
  179. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  180. height_field='mugshot_height',
  181. width_field='mugshot_width')
  182. headshot_height = models.PositiveSmallIntegerField(
  183. blank=True, null=True)
  184. headshot_width = models.PositiveSmallIntegerField(
  185. blank=True, null=True)
  186. headshot = TestImageField(blank=True, null=True,
  187. storage=temp_storage, upload_to='tests',
  188. height_field='headshot_height',
  189. width_field='headshot_width')
  190. ###############################################################################