1. import datetime
    
  2. import itertools
    
  3. import tempfile
    
  4. 
    
  5. from django.core.files.storage import FileSystemStorage
    
  6. from django.db import models
    
  7. 
    
  8. callable_default_counter = itertools.count()
    
  9. 
    
  10. 
    
  11. def callable_default():
    
  12.     return next(callable_default_counter)
    
  13. 
    
  14. 
    
  15. temp_storage = FileSystemStorage(location=tempfile.mkdtemp())
    
  16. 
    
  17. 
    
  18. class BoundaryModel(models.Model):
    
  19.     positive_integer = models.PositiveIntegerField(null=True, blank=True)
    
  20. 
    
  21. 
    
  22. class Defaults(models.Model):
    
  23.     name = models.CharField(max_length=255, default="class default value")
    
  24.     def_date = models.DateField(default=datetime.date(1980, 1, 1))
    
  25.     value = models.IntegerField(default=42)
    
  26.     callable_default = models.IntegerField(default=callable_default)
    
  27. 
    
  28. 
    
  29. class ChoiceModel(models.Model):
    
  30.     """For ModelChoiceField and ModelMultipleChoiceField tests."""
    
  31. 
    
  32.     CHOICES = [
    
  33.         ("", "No Preference"),
    
  34.         ("f", "Foo"),
    
  35.         ("b", "Bar"),
    
  36.     ]
    
  37. 
    
  38.     INTEGER_CHOICES = [
    
  39.         (None, "No Preference"),
    
  40.         (1, "Foo"),
    
  41.         (2, "Bar"),
    
  42.     ]
    
  43. 
    
  44.     STRING_CHOICES_WITH_NONE = [
    
  45.         (None, "No Preference"),
    
  46.         ("f", "Foo"),
    
  47.         ("b", "Bar"),
    
  48.     ]
    
  49. 
    
  50.     name = models.CharField(max_length=10)
    
  51.     choice = models.CharField(max_length=2, blank=True, choices=CHOICES)
    
  52.     choice_string_w_none = models.CharField(
    
  53.         max_length=2, blank=True, null=True, choices=STRING_CHOICES_WITH_NONE
    
  54.     )
    
  55.     choice_integer = models.IntegerField(choices=INTEGER_CHOICES, blank=True, null=True)
    
  56. 
    
  57. 
    
  58. class ChoiceOptionModel(models.Model):
    
  59.     """
    
  60.     Destination for ChoiceFieldModel's ForeignKey.
    
  61.     Can't reuse ChoiceModel because error_message tests require that it have no
    
  62.     instances.
    
  63.     """
    
  64. 
    
  65.     name = models.CharField(max_length=10)
    
  66. 
    
  67.     class Meta:
    
  68.         ordering = ("name",)
    
  69. 
    
  70.     def __str__(self):
    
  71.         return "ChoiceOption %d" % self.pk
    
  72. 
    
  73. 
    
  74. def choice_default():
    
  75.     return ChoiceOptionModel.objects.get_or_create(name="default")[0].pk
    
  76. 
    
  77. 
    
  78. def choice_default_list():
    
  79.     return [choice_default()]
    
  80. 
    
  81. 
    
  82. def int_default():
    
  83.     return 1
    
  84. 
    
  85. 
    
  86. def int_list_default():
    
  87.     return [1]
    
  88. 
    
  89. 
    
  90. class ChoiceFieldModel(models.Model):
    
  91.     """Model with ForeignKey to another model, for testing ModelForm
    
  92.     generation with ModelChoiceField."""
    
  93. 
    
  94.     choice = models.ForeignKey(
    
  95.         ChoiceOptionModel,
    
  96.         models.CASCADE,
    
  97.         blank=False,
    
  98.         default=choice_default,
    
  99.     )
    
  100.     choice_int = models.ForeignKey(
    
  101.         ChoiceOptionModel,
    
  102.         models.CASCADE,
    
  103.         blank=False,
    
  104.         related_name="choice_int",
    
  105.         default=int_default,
    
  106.     )
    
  107.     multi_choice = models.ManyToManyField(
    
  108.         ChoiceOptionModel,
    
  109.         blank=False,
    
  110.         related_name="multi_choice",
    
  111.         default=choice_default_list,
    
  112.     )
    
  113.     multi_choice_int = models.ManyToManyField(
    
  114.         ChoiceOptionModel,
    
  115.         blank=False,
    
  116.         related_name="multi_choice_int",
    
  117.         default=int_list_default,
    
  118.     )
    
  119. 
    
  120. 
    
  121. class OptionalMultiChoiceModel(models.Model):
    
  122.     multi_choice = models.ManyToManyField(
    
  123.         ChoiceOptionModel,
    
  124.         blank=False,
    
  125.         related_name="not_relevant",
    
  126.         default=choice_default,
    
  127.     )
    
  128.     multi_choice_optional = models.ManyToManyField(
    
  129.         ChoiceOptionModel,
    
  130.         blank=True,
    
  131.         related_name="not_relevant2",
    
  132.     )
    
  133. 
    
  134. 
    
  135. class FileModel(models.Model):
    
  136.     file = models.FileField(storage=temp_storage, upload_to="tests")
    
  137. 
    
  138. 
    
  139. class Article(models.Model):
    
  140.     content = models.TextField()