1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #24919 -- Made test models serializable for migrations.

This commit is contained in:
Tim Graham 2015-07-15 21:18:07 -04:00
parent 8a5eadd140
commit 04e69598de
6 changed files with 72 additions and 38 deletions

View File

@ -422,10 +422,12 @@ class Category(models.Model):
return '%s:o%s' % (self.id, self.order)
def link_posted_default():
return datetime.date.today() - datetime.timedelta(days=7)
class Link(models.Model):
posted = models.DateField(
default=lambda: datetime.date.today() - datetime.timedelta(days=7)
)
posted = models.DateField(default=link_posted_default)
url = models.URLField()
post = models.ForeignKey("Post")

View File

@ -12,7 +12,8 @@ class R(models.Model):
return "%s" % self.pk
get_default_r = lambda: R.objects.get_or_create(is_default=True)[0]
def get_default_r():
return R.objects.get_or_create(is_default=True)[0]
class S(models.Model):

View File

@ -5,6 +5,7 @@ import warnings
from django.db import models
from django.utils import six
from django.utils.deconstruct import deconstructible
from django.utils.deprecation import RemovedInDjango110Warning
from django.utils.encoding import force_text, python_2_unicode_compatible
@ -16,6 +17,7 @@ warnings.filterwarnings(
)
@deconstructible
@python_2_unicode_compatible
class Small(object):
"""

View File

@ -10,7 +10,10 @@ from django.db import models
from django.utils.encoding import python_2_unicode_compatible
callable_default_counter = itertools.count()
callable_default = lambda: next(callable_default_counter)
def callable_default():
return next(callable_default_counter)
temp_storage = FileSystemStorage(location=tempfile.mkdtemp())
@ -68,25 +71,62 @@ class ChoiceOptionModel(models.Model):
return 'ChoiceOption %d' % self.pk
def choice_default():
return ChoiceOptionModel.objects.get_or_create(name='default')[0]
def choice_default_list():
return [choice_default()]
def int_default():
return 1
def int_list_default():
return [1]
class ChoiceFieldModel(models.Model):
"""Model with ForeignKey to another model, for testing ModelForm
generation with ModelChoiceField."""
choice = models.ForeignKey(ChoiceOptionModel, blank=False,
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])
choice = models.ForeignKey(
ChoiceOptionModel,
blank=False,
default=choice_default,
)
choice_int = models.ForeignKey(
ChoiceOptionModel,
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,
)
class OptionalMultiChoiceModel(models.Model):
multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='not_relevant',
default=lambda: ChoiceOptionModel.objects.filter(name='default'))
multi_choice_optional = models.ManyToManyField(ChoiceOptionModel, blank=True,
related_name='not_relevant2')
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',
)
class FileModel(models.Model):

View File

@ -1,6 +1,7 @@
import datetime
from django.db import DJANGO_VERSION_PICKLE_KEY, models
from django.utils import six
from django.utils.translation import ugettext_lazy as _
@ -13,15 +14,6 @@ class Numbers(object):
def get_static_number():
return 2
@classmethod
def get_class_number(cls):
return 3
def get_member_number(self):
return 4
nn = Numbers()
class PreviousDjangoVersionQuerySet(models.QuerySet):
def __getstate__(self):
@ -51,11 +43,11 @@ class Event(models.Model):
class Happening(models.Model):
when = models.DateTimeField(blank=True, default=datetime.datetime.now)
name = models.CharField(blank=True, max_length=100, default=lambda: "test")
name = models.CharField(blank=True, max_length=100, default="test")
number1 = models.IntegerField(blank=True, default=standalone_number)
if six.PY3:
# default serializable on Python 3 only
number2 = models.IntegerField(blank=True, default=Numbers.get_static_number)
number3 = models.IntegerField(blank=True, default=Numbers.get_class_number)
number4 = models.IntegerField(blank=True, default=nn.get_member_number)
class Container(object):

View File

@ -2,9 +2,11 @@ from __future__ import unicode_literals
import datetime
import pickle
import unittest
import warnings
from django.test import TestCase
from django.utils import six
from django.utils.encoding import force_text
from django.utils.version import get_version
@ -28,21 +30,16 @@ class PickleabilityTestCase(TestCase):
def test_datetime_callable_default_filter(self):
self.assert_pickles(Happening.objects.filter(when=datetime.datetime.now()))
def test_lambda_as_default(self):
def test_string_as_default(self):
self.assert_pickles(Happening.objects.filter(name="test"))
def test_standalone_method_as_default(self):
self.assert_pickles(Happening.objects.filter(number1=1))
@unittest.skipIf(six.PY2, "Field doesn't exist on Python 2.")
def test_staticmethod_as_default(self):
self.assert_pickles(Happening.objects.filter(number2=1))
def test_classmethod_as_default(self):
self.assert_pickles(Happening.objects.filter(number3=1))
def test_membermethod_as_default(self):
self.assert_pickles(Happening.objects.filter(number4=1))
def test_filter_reverse_fk(self):
self.assert_pickles(Group.objects.filter(event=1))