Fixed #25129 -- Made model instance defaults work with migrations (refs #24919).

This commit is contained in:
Tim Graham 2015-07-16 08:00:29 -04:00
parent 04e69598de
commit b60375d4bb
5 changed files with 9 additions and 5 deletions

View File

@ -220,6 +220,10 @@ Note that ``lambda``\s cannot be used for field options like ``default``
because they cannot be :ref:`serialized by migrations <migration-serializing>`.
See that documentation for other caveats.
For fields like :class:`ForeignKey` that map to model instances, defaults
should be the value of the field they reference (``pk`` unless
:attr:`~ForeignKey.to_field` is set) instead of model instances.
The default value is used when new model instances are created and a value
isn't provided for the field. When the field is a primary key, the default is
also used when the field is set to ``None``.

View File

@ -13,7 +13,7 @@ class R(models.Model):
def get_default_r():
return R.objects.get_or_create(is_default=True)[0]
return R.objects.get_or_create(is_default=True)[0].pk
class S(models.Model):

View File

@ -31,7 +31,7 @@ class OnDeleteTests(TestCase):
a = create_a('setvalue')
a.setvalue.delete()
a = A.objects.get(pk=a.pk)
self.assertEqual(self.DEFAULT, a.setvalue)
self.assertEqual(self.DEFAULT, a.setvalue.pk)
def test_setnull(self):
a = create_a('setnull')
@ -43,7 +43,7 @@ class OnDeleteTests(TestCase):
a = create_a('setdefault')
a.setdefault.delete()
a = A.objects.get(pk=a.pk)
self.assertEqual(self.DEFAULT, a.setdefault)
self.assertEqual(self.DEFAULT, a.setdefault.pk)
def test_setdefault_none(self):
a = create_a('setdefault_none')

View File

@ -72,7 +72,7 @@ class ChoiceOptionModel(models.Model):
def choice_default():
return ChoiceOptionModel.objects.get_or_create(name='default')[0]
return ChoiceOptionModel.objects.get_or_create(name='default')[0].pk
def choice_default_list():

View File

@ -26,7 +26,7 @@ class Foo(models.Model):
def get_foo():
return Foo.objects.get(id=1)
return Foo.objects.get(id=1).pk
class Bar(models.Model):