Ticket #10750: 10750_baseinlineformset_savenew.2.diff
File 10750_baseinlineformset_savenew.2.diff, 2.2 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
546 546 # save the object. 547 547 obj = form.save(commit=False) 548 548 setattr(obj, self.fk.get_attname(), self.instance.pk) 549 obj.save() 549 if commit: 550 obj.save() 550 551 # form.save_m2m() can be called via the formset later on if commit=False 551 552 if commit and hasattr(form, 'save_m2m'): 552 553 form.save_m2m() -
tests/regressiontests/inline_formsets/tests.py
1 1 from django.test import TestCase 2 2 from django.forms.models import inlineformset_factory 3 from regressiontests.inline_formsets.models import Poet, Poem 3 from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child 4 4 5 5 class DeletionTests(TestCase): 6 6 def test_deletion(self): … … 74 74 self.assertEqual(formset.is_valid(), True) 75 75 formset.save() 76 76 self.assertEqual(Poem.objects.count(), 0) 77 78 def test_save_new(self): 79 """ 80 Make sure inlineformsets respect commit=False 81 regression for #10750 82 """ 83 # exclude some required field from the forms 84 ChildFormSet = inlineformset_factory(School, Child, exclude=['father', 'mother']) 85 school = School.objects.create(name=u'test') 86 mother = Parent.objects.create(name=u'mother') 87 father = Parent.objects.create(name=u'father') 88 data = { 89 'child_set-TOTAL_FORMS': u'1', 90 'child_set-INITIAL_FORMS': u'0', 91 'child_set-0-name': u'child', 92 } 93 formset = ChildFormSet(data, instance=school) 94 self.assertEqual(formset.is_valid(), True) 95 objects = formset.save(commit=False) 96 for obj in objects: 97 obj.mother = mother 98 obj.father = father 99 obj.save() 100 self.assertEqual(school.child_set.count(), 1) 101