2008-07-20 02:47:59 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2007-10-20 21:01:40 +08:00
|
|
|
import datetime
|
2014-11-19 04:52:26 +08:00
|
|
|
import itertools
|
|
|
|
import os
|
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
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2009-01-17 05:31:58 +08:00
|
|
|
|
2010-11-19 06:43:46 +08:00
|
|
|
|
2014-11-19 04:52:26 +08:00
|
|
|
callable_default_counter = itertools.count()
|
|
|
|
callable_default = lambda: next(callable_default_counter)
|
|
|
|
|
|
|
|
|
2011-11-14 03:05:02 +08:00
|
|
|
temp_storage_location = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
2009-01-17 05:31:58 +08:00
|
|
|
temp_storage = FileSystemStorage(location=temp_storage_location)
|
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."""
|
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)
|
|
|
|
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
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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."""
|
|
|
|
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
|
|
|
|
2009-05-10 15:44:27 +08:00
|
|
|
class ChoiceFieldModel(models.Model):
|
|
|
|
"""Model with ForeignKey to another model, for testing ModelForm
|
|
|
|
generation with ModelChoiceField."""
|
|
|
|
choice = models.ForeignKey(ChoiceOptionModel, blank=False,
|
2010-08-14 20:05:41 +08:00
|
|
|
default=lambda: ChoiceOptionModel.objects.get(name='default'))
|
|
|
|
choice_int = models.ForeignKey(ChoiceOptionModel, blank=False, related_name='choice_int',
|
|
|
|
default=lambda: 1)
|
|
|
|
|
|
|
|
multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice',
|
|
|
|
default=lambda: ChoiceOptionModel.objects.filter(name='default'))
|
|
|
|
multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int',
|
|
|
|
default=lambda: [1])
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-10-04 00:50:12 +08:00
|
|
|
class OptionalMultiChoiceModel(models.Model):
|
|
|
|
multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='not_relevant',
|
|
|
|
default=lambda: ChoiceOptionModel.objects.filter(name='default'))
|
2014-07-09 04:42:40 +08:00
|
|
|
multi_choice_optional = models.ManyToManyField(ChoiceOptionModel, blank=True,
|
2012-10-04 00:50:12 +08:00
|
|
|
related_name='not_relevant2')
|
|
|
|
|
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-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2010-01-12 22:58:24 +08:00
|
|
|
class Group(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s' % self.name
|
2010-11-19 06:43:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Cheese(models.Model):
|
2012-11-06 03:27:06 +08:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
content = models.TextField()
|