2007-10-20 21:01:40 +08:00
|
|
|
import datetime
|
2014-11-19 04:52:26 +08:00
|
|
|
import itertools
|
2010-10-12 11:33:19 +08:00
|
|
|
import tempfile
|
2007-10-20 21:01:40 +08:00
|
|
|
|
2009-01-17 05:31:58 +08:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.db import models
|
2009-01-17 05:31:58 +08:00
|
|
|
|
2014-11-19 04:52:26 +08:00
|
|
|
callable_default_counter = itertools.count()
|
2015-07-16 09:18:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def callable_default():
|
|
|
|
return next(callable_default_counter)
|
2014-11-19 04:52:26 +08:00
|
|
|
|
|
|
|
|
2015-02-22 01:56:36 +08:00
|
|
|
temp_storage = FileSystemStorage(location=tempfile.mkdtemp())
|
2007-09-15 10:37:07 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2007-10-20 21:01:40 +08:00
|
|
|
class BoundaryModel(models.Model):
|
2007-09-15 10:37:07 +08:00
|
|
|
positive_integer = models.PositiveIntegerField(null=True, blank=True)
|
2007-10-20 21:01:40 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2007-10-20 21:01:40 +08:00
|
|
|
class Defaults(models.Model):
|
2009-05-08 02:06:22 +08:00
|
|
|
name = models.CharField(max_length=255, default="class default value")
|
2013-11-03 12:36:09 +08:00
|
|
|
def_date = models.DateField(default=datetime.date(1980, 1, 1))
|
2007-10-20 21:01:40 +08:00
|
|
|
value = models.IntegerField(default=42)
|
2010-03-08 23:03:30 +08:00
|
|
|
callable_default = models.IntegerField(default=callable_default)
|
2007-10-20 21:01:40 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2007-11-19 04:25:23 +08:00
|
|
|
class ChoiceModel(models.Model):
|
|
|
|
"""For ModelChoiceField and ModelMultipleChoiceField tests."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-07-21 05:49:33 +08:00
|
|
|
CHOICES = [
|
|
|
|
("", "No Preference"),
|
|
|
|
("f", "Foo"),
|
|
|
|
("b", "Bar"),
|
|
|
|
]
|
|
|
|
|
|
|
|
INTEGER_CHOICES = [
|
|
|
|
(None, "No Preference"),
|
|
|
|
(1, "Foo"),
|
|
|
|
(2, "Bar"),
|
|
|
|
]
|
|
|
|
|
|
|
|
STRING_CHOICES_WITH_NONE = [
|
|
|
|
(None, "No Preference"),
|
|
|
|
("f", "Foo"),
|
|
|
|
("b", "Bar"),
|
|
|
|
]
|
|
|
|
|
2007-11-19 04:25:23 +08:00
|
|
|
name = models.CharField(max_length=10)
|
2013-07-21 05:49:33 +08:00
|
|
|
choice = models.CharField(max_length=2, blank=True, choices=CHOICES)
|
|
|
|
choice_string_w_none = models.CharField(
|
|
|
|
max_length=2, blank=True, null=True, choices=STRING_CHOICES_WITH_NONE
|
|
|
|
)
|
2018-03-16 17:54:34 +08:00
|
|
|
choice_integer = models.IntegerField(choices=INTEGER_CHOICES, blank=True, null=True)
|
2007-11-19 04:25:23 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2009-05-10 15:44:27 +08:00
|
|
|
class ChoiceOptionModel(models.Model):
|
|
|
|
"""
|
|
|
|
Destination for ChoiceFieldModel's ForeignKey.
|
|
|
|
Can't reuse ChoiceModel because error_message tests require that it have no
|
|
|
|
instances.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2009-05-10 15:44:27 +08:00
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2010-08-14 20:05:41 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "ChoiceOption %d" % self.pk
|
2010-08-14 20:05:41 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2015-07-16 09:18:07 +08:00
|
|
|
def choice_default():
|
2015-07-16 20:00:29 +08:00
|
|
|
return ChoiceOptionModel.objects.get_or_create(name="default")[0].pk
|
2015-07-16 09:18:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def choice_default_list():
|
|
|
|
return [choice_default()]
|
|
|
|
|
|
|
|
|
|
|
|
def int_default():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
def int_list_default():
|
|
|
|
return [1]
|
|
|
|
|
|
|
|
|
2009-05-10 15:44:27 +08:00
|
|
|
class ChoiceFieldModel(models.Model):
|
|
|
|
"""Model with ForeignKey to another model, for testing ModelForm
|
|
|
|
generation with ModelChoiceField."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-16 09:18:07 +08:00
|
|
|
choice = models.ForeignKey(
|
|
|
|
ChoiceOptionModel,
|
2015-07-22 22:43:21 +08:00
|
|
|
models.CASCADE,
|
2015-07-16 09:18:07 +08:00
|
|
|
blank=False,
|
|
|
|
default=choice_default,
|
|
|
|
)
|
|
|
|
choice_int = models.ForeignKey(
|
|
|
|
ChoiceOptionModel,
|
2015-07-22 22:43:21 +08:00
|
|
|
models.CASCADE,
|
2015-07-16 09:18:07 +08:00
|
|
|
blank=False,
|
|
|
|
related_name="choice_int",
|
|
|
|
default=int_default,
|
|
|
|
)
|
|
|
|
multi_choice = models.ManyToManyField(
|
|
|
|
ChoiceOptionModel,
|
|
|
|
blank=False,
|
|
|
|
related_name="multi_choice",
|
|
|
|
default=choice_default_list,
|
|
|
|
)
|
|
|
|
multi_choice_int = models.ManyToManyField(
|
|
|
|
ChoiceOptionModel,
|
|
|
|
blank=False,
|
|
|
|
related_name="multi_choice_int",
|
|
|
|
default=int_list_default,
|
|
|
|
)
|
2010-08-14 20:05:41 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-10-04 00:50:12 +08:00
|
|
|
class OptionalMultiChoiceModel(models.Model):
|
2015-07-16 09:18:07 +08:00
|
|
|
multi_choice = models.ManyToManyField(
|
|
|
|
ChoiceOptionModel,
|
|
|
|
blank=False,
|
|
|
|
related_name="not_relevant",
|
|
|
|
default=choice_default,
|
|
|
|
)
|
|
|
|
multi_choice_optional = models.ManyToManyField(
|
|
|
|
ChoiceOptionModel,
|
|
|
|
blank=True,
|
|
|
|
related_name="not_relevant2",
|
|
|
|
)
|
2012-10-04 00:50:12 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2008-07-20 02:47:59 +08:00
|
|
|
class FileModel(models.Model):
|
2009-01-17 05:31:58 +08:00
|
|
|
file = models.FileField(storage=temp_storage, upload_to="tests")
|
2008-07-20 02:47:59 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2012-11-06 03:27:06 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
content = models.TextField()
|