summaryrefslogtreecommitdiff
path: root/tests/model_formsets
diff options
context:
space:
mode:
authorHiroki Kiyohara <hirokiky@gmail.com>2016-12-07 03:06:58 +0900
committerTim Graham <timograham@gmail.com>2016-12-06 13:06:58 -0500
commit181f492ad021aeb43105aa9d38106ad7baf00211 (patch)
tree4d9196fe20853ef3c2b741120fdc8b3e992765c8 /tests/model_formsets
parent373140b07aa452946ccb6d5b0317fa09ed5bbdc2 (diff)
Fixed #27416 -- Prevented ModelFormSet from creating objects for invalid PKs in data.
Diffstat (limited to 'tests/model_formsets')
-rw-r--r--tests/model_formsets/tests.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
index fc0dff8304..4387fbf52d 100644
--- a/tests/model_formsets/tests.py
+++ b/tests/model_formsets/tests.py
@@ -1631,6 +1631,28 @@ class ModelFormsetTest(TestCase):
['Please correct the duplicate data for subtitle which must be unique for the month in posted.']
)
+ def test_prevent_change_outer_model_and_create_invalid_data(self):
+ author = Author.objects.create(name='Charles')
+ other_author = Author.objects.create(name='Walt')
+ AuthorFormSet = modelformset_factory(Author, fields='__all__')
+ data = {
+ 'form-TOTAL_FORMS': '2',
+ 'form-INITIAL_FORMS': '2',
+ 'form-MAX_NUM_FORMS': '',
+ 'form-0-id': str(author.id),
+ 'form-0-name': 'Charles',
+ 'form-1-id': str(other_author.id), # A model not in the formset's queryset.
+ 'form-1-name': 'Changed name',
+ }
+ # This formset is only for Walt Whitman and shouldn't accept data for
+ # other_author.
+ formset = AuthorFormSet(data=data, queryset=Author.objects.filter(id__in=(author.id,)))
+ self.assertTrue(formset.is_valid())
+ formset.save()
+ # The name of other_author shouldn't be changed and new models aren't
+ # created.
+ self.assertQuerysetEqual(Author.objects.all(), ['<Author: Charles>', '<Author: Walt>'])
+
class TestModelFormsetOverridesTroughFormMeta(TestCase):
def test_modelformset_factory_widgets(self):