Ticket #7230: 7230_formset_save_m2m.diff

File 7230_formset_save_m2m.diff, 1.5 KB (added by brosner, 7 years ago)
  • django/newforms/models.py

    diff --git a/django/newforms/models.py b/django/newforms/models.py
    index e515071..1c10d20 100644
    a b class BaseModelFormSet(BaseFormSet): 
    330330        """Saves model instances for every form, adding and changing instances
    331331        as necessary, and returns the list of instances.
    332332        """
     333        if not commit:
     334            self.saved_forms = []
     335            def save_m2m(self):
     336                for form in self.saved_forms:
     337                    form.save_m2m()
     338            self.save_m2m = save_m2m
    333339        return self.save_existing_objects(commit) + self.save_new_objects(commit)
    334340
    335341    def save_existing_objects(self, commit=True):
    class BaseModelFormSet(BaseFormSet): 
    352358                if form.changed_data:
    353359                    self.changed_objects.append((obj, form.changed_data))
    354360                    saved_instances.append(self.save_existing(form, obj, commit=commit))
     361                    if not commit:
     362                        self.saved_forms.append(form)
    355363        return saved_instances
    356364
    357365    def save_new_objects(self, commit=True):
    class BaseModelFormSet(BaseFormSet): 
    364372            if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
    365373                continue
    366374            self.new_objects.append(self.save_new(form, commit=commit))
     375            if not commit:
     376                self.saved_forms.append(form)
    367377        return self.new_objects
    368378
    369379    def add_fields(self, form, index):
Back to Top