Ticket #10750: 10750_baseinlineformset_savenew.2.diff

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

new try, replace messed up diff

  • 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

     
    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):
     
    7474        self.assertEqual(formset.is_valid(), True)
    7575        formset.save()
    7676        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
Back to Top