diff options
author | Tim Graham <timograham@gmail.com> | 2015-02-20 14:28:34 -0500 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-02-23 08:44:27 -0500 |
commit | 1306cd1e8acfb13602ee8dc40993b2505cd7523b (patch) | |
tree | c73bb367f295e54403a24b27fe3878c80ef8756e /tests/model_formsets | |
parent | 00fbd8fd527fd6ee0319361bf194cd51a1ac1bfb (diff) | |
download | django-1306cd1e8acfb13602ee8dc40993b2505cd7523b.tar.gz |
Fixed #24377 -- Fixed model inline formsets with primary key's that have defaults.
Diffstat (limited to 'tests/model_formsets')
-rw-r--r-- | tests/model_formsets/models.py | 13 | ||||
-rw-r--r-- | tests/model_formsets/test_uuid.py | 32 |
2 files changed, 45 insertions, 0 deletions
diff --git a/tests/model_formsets/models.py b/tests/model_formsets/models.py index da79c6a573..0b4963208f 100644 --- a/tests/model_formsets/models.py +++ b/tests/model_formsets/models.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import datetime +import uuid from django.db import models from django.utils import six @@ -241,3 +242,15 @@ class Post(models.Model): def __str__(self): return self.name + + +# Models for testing UUID primary keys +class UUIDPKParent(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=255) + + +class UUIDPKChild(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=255) + parent = models.ForeignKey(UUIDPKParent) diff --git a/tests/model_formsets/test_uuid.py b/tests/model_formsets/test_uuid.py new file mode 100644 index 0000000000..8025f758c9 --- /dev/null +++ b/tests/model_formsets/test_uuid.py @@ -0,0 +1,32 @@ +from django.forms.models import inlineformset_factory +from django.test import TestCase + +from .models import UUIDPKChild, UUIDPKParent + + +class InlineFormsetTests(TestCase): + def test_inlineformset_factory_nulls_default_pks(self): + """ + #24377 - If we're adding a new object, a parent's auto-generated pk + from the model field default should be ignored as it's regenerated on + the save request. + """ + FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__') + formset = FormSet() + self.assertIsNone(formset.forms[0].fields['parent'].initial) + + def test_inlineformset_factory_ignores_default_pks_on_submit(self): + """ + #24377 - Inlines with a model field default should ignore that default + value to avoid triggering validation on empty forms. + """ + FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__') + formset = FormSet({ + 'uuidpkchild_set-TOTAL_FORMS': 3, + 'uuidpkchild_set-INITIAL_FORMS': 0, + 'uuidpkchild_set-MAX_NUM_FORMS': '', + 'uuidpkchild_set-0-name': 'Foo', + 'uuidpkchild_set-1-name': '', + 'uuidpkchild_set-2-name': '', + }) + self.assertTrue(formset.is_valid()) |