models.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import os
  2. import tempfile
  3. from django.core.exceptions import ImproperlyConfigured
  4. try:
  5. from django.utils.image import Image
  6. except ImproperlyConfigured:
  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 BigS(models.Model):
  37. s = models.SlugField(max_length=255)
  38. class BigInt(models.Model):
  39. value = models.BigIntegerField()
  40. null_value = models.BigIntegerField(null = True, blank = True)
  41. class Post(models.Model):
  42. title = models.CharField(max_length=100)
  43. body = models.TextField()
  44. class NullBooleanModel(models.Model):
  45. nbfield = models.NullBooleanField()
  46. class BooleanModel(models.Model):
  47. bfield = models.BooleanField()
  48. string = models.CharField(max_length=10, default='abc')
  49. class FksToBooleans(models.Model):
  50. """Model wih FKs to models with {Null,}BooleanField's, #15040"""
  51. bf = models.ForeignKey(BooleanModel)
  52. nbf = models.ForeignKey(NullBooleanModel)
  53. class RenamedField(models.Model):
  54. modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
  55. class VerboseNameField(models.Model):
  56. id = models.AutoField("verbose pk", primary_key=True)
  57. field1 = models.BigIntegerField("verbose field1")
  58. field2 = models.BooleanField("verbose field2")
  59. field3 = models.CharField("verbose field3", max_length=10)
  60. field4 = models.CommaSeparatedIntegerField("verbose field4", max_length=99)
  61. field5 = models.DateField("verbose field5")
  62. field6 = models.DateTimeField("verbose field6")
  63. field7 = models.DecimalField("verbose field7", max_digits=6, decimal_places=1)
  64. field8 = models.EmailField("verbose field8")
  65. field9 = models.FileField("verbose field9", upload_to="unused")
  66. field10 = models.FilePathField("verbose field10")
  67. field11 = models.FloatField("verbose field11")
  68. # Don't want to depend on Pillow/PIL in this test
  69. #field_image = models.ImageField("verbose field")
  70. field12 = models.IntegerField("verbose field12")
  71. field13 = models.IPAddressField("verbose field13")
  72. field14 = models.GenericIPAddressField("verbose field14", protocol="ipv4")
  73. field15 = models.NullBooleanField("verbose field15")
  74. field16 = models.PositiveIntegerField("verbose field16")
  75. field17 = models.PositiveSmallIntegerField("verbose field17")
  76. field18 = models.SlugField("verbose field18")
  77. field19 = models.SmallIntegerField("verbose field19")
  78. field20 = models.TextField("verbose field20")
  79. field21 = models.TimeField("verbose field21")
  80. field22 = models.URLField("verbose field22")
  81. # This model isn't used in any test, just here to ensure it validates successfully.
  82. # See ticket #16570.
  83. class DecimalLessThanOne(models.Model):
  84. d = models.DecimalField(max_digits=3, decimal_places=3)
  85. class DataModel(models.Model):
  86. short_data = models.BinaryField(max_length=10, default=b'\x08')
  87. data = models.BinaryField()
  88. ###############################################################################
  89. # FileField
  90. class Document(models.Model):
  91. myfile = models.FileField(upload_to='unused')
  92. ###############################################################################
  93. # ImageField
  94. # If Pillow/PIL available, do these tests.
  95. if Image:
  96. class TestImageFieldFile(ImageFieldFile):
  97. """
  98. Custom Field File class that records whether or not the underlying file
  99. was opened.
  100. """
  101. def __init__(self, *args, **kwargs):
  102. self.was_opened = False
  103. super(TestImageFieldFile, self).__init__(*args,**kwargs)
  104. def open(self):
  105. self.was_opened = True
  106. super(TestImageFieldFile, self).open()
  107. class TestImageField(ImageField):
  108. attr_class = TestImageFieldFile
  109. # Set up a temp directory for file storage.
  110. temp_storage_dir = tempfile.mkdtemp()
  111. temp_storage = FileSystemStorage(temp_storage_dir)
  112. temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
  113. class Person(models.Model):
  114. """
  115. Model that defines an ImageField with no dimension fields.
  116. """
  117. name = models.CharField(max_length=50)
  118. mugshot = TestImageField(storage=temp_storage, upload_to='tests')
  119. class PersonWithHeight(models.Model):
  120. """
  121. Model that defines an ImageField with only one dimension field.
  122. """
  123. name = models.CharField(max_length=50)
  124. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  125. height_field='mugshot_height')
  126. mugshot_height = models.PositiveSmallIntegerField()
  127. class PersonWithHeightAndWidth(models.Model):
  128. """
  129. Model that defines height and width fields after the ImageField.
  130. """
  131. name = models.CharField(max_length=50)
  132. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  133. height_field='mugshot_height',
  134. width_field='mugshot_width')
  135. mugshot_height = models.PositiveSmallIntegerField()
  136. mugshot_width = models.PositiveSmallIntegerField()
  137. class PersonDimensionsFirst(models.Model):
  138. """
  139. Model that defines height and width fields before the ImageField.
  140. """
  141. name = models.CharField(max_length=50)
  142. mugshot_height = models.PositiveSmallIntegerField()
  143. mugshot_width = models.PositiveSmallIntegerField()
  144. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  145. height_field='mugshot_height',
  146. width_field='mugshot_width')
  147. class PersonTwoImages(models.Model):
  148. """
  149. Model that:
  150. * Defines two ImageFields
  151. * Defines the height/width fields before the ImageFields
  152. * Has a nullalble ImageField
  153. """
  154. name = models.CharField(max_length=50)
  155. mugshot_height = models.PositiveSmallIntegerField()
  156. mugshot_width = models.PositiveSmallIntegerField()
  157. mugshot = TestImageField(storage=temp_storage, upload_to='tests',
  158. height_field='mugshot_height',
  159. width_field='mugshot_width')
  160. headshot_height = models.PositiveSmallIntegerField(
  161. blank=True, null=True)
  162. headshot_width = models.PositiveSmallIntegerField(
  163. blank=True, null=True)
  164. headshot = TestImageField(blank=True, null=True,
  165. storage=temp_storage, upload_to='tests',
  166. height_field='headshot_height',
  167. width_field='headshot_width')
  168. ###############################################################################