Ticket #10750: 10750_baseinlineformset_savenew.diff

File 10750_baseinlineformset_savenew.diff, 2.2 KB (added by Koen Biermans <koen.biermans@…>, 16 years ago)

fix and test for baseinlineformset save not respecting commit=False

  • django/forms/models.py

     
    546546        # save the object.
    547547        obj = form.save(commit=False)
    548548        setattr(obj, self.fk.get_attname(), self.instance.pk)
    549         obj.save()
     549        if commit:
     550            obj.save()
    550551        # form.save_m2m() can be called via the formset later on if commit=False
    551552        if commit and hasattr(form, 'save_m2m'):
    552553            form.save_m2m()
  • tests/regressiontests/inline_formsets/tests.py

    (this hunk was shorter than expected)  
    11from django.test import TestCase
    22from django.forms.models import inlineformset_factory
    3 from regressiontests.inline_formsets.models import Poet, Poem
     3from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child
    44
    55class DeletionTests(TestCase):
    66    def test_deletion(self):
     
    7475        self.assertEqual(formset.is_valid(), True)
    7576        formset.save()
    7677        self.assertEqual(Poem.objects.count(), 0)
     78
     79    def test_save_new(self):
     80        """
     81        Make sure inlineformsets respect commit=False
     82        regression for #10750
     83        """
     84        # exclude some required field from the forms
     85        ChildFormSet = inlineformset_factory(School, Child, exclude=['father', 'mother'])
     86        school = School.objects.create(name=u'test')
     87        mother = Parent.objects.create(name=u'mother')
     88        father = Parent.objects.create(name=u'father')
     89        data = {
     90            'child_set-TOTAL_FORMS': u'1',
     91            'child_set-INITIAL_FORMS': u'0',
     92            'child_set-0-name': u'child',
     93        }
     94        formset = ChildFormSet(data, instance=school)
     95        self.assertEqual(formset.is_valid(), True)
     96        objects = formset.save(commit=False)
     97        for obj in objects:
     98            obj.mother = mother
     99            obj.father = father
     100            obj.save()
     101        self.assertEqual(school.child_set.count(), 1)
     102
Back to Top