Ticket #12507: 12507.diff

File 12507.diff, 3.2 KB (added by jkocherhans, 6 years ago)

Sorry, but combine this with the patch from #12521. Different problems, dependent solution.

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index c18af62..fbd5e95 100644
    a b class ModelAdmin(BaseModelAdmin): 
    578578        """
    579579        messages.info(request, message)
    580580
    581     def save_form(self, request, form, change, commit=False):
     581    def save_form(self, request, form, change):
    582582        """
    583583        Given a ModelForm return an unsaved instance. ``change`` is True if
    584584        the object is being changed, and False if it's being added.
    585585        """
    586         return form.save(commit=commit)
     586        return form.save(commit=False)
    587587
    588588    def save_model(self, request, obj, form, change):
    589589        """
    class ModelAdmin(BaseModelAdmin): 
    757757        if request.method == 'POST':
    758758            form = ModelForm(request.POST, request.FILES)
    759759            if form.is_valid():
    760                 # Save the object, even if inline formsets haven't been
    761                 # validated yet. We need to pass the valid model to the
    762                 # formsets for validation. If the formsets do not validate, we
    763                 # will delete the object.
    764                 new_object = self.save_form(request, form, change=False, commit=True)
     760                new_object = self.save_form(request, form, change=False)
    765761                form_validated = True
    766762            else:
    767763                form_validated = False
    class ModelAdmin(BaseModelAdmin): 
    778774                                  prefix=prefix, queryset=inline.queryset(request))
    779775                formsets.append(formset)
    780776            if all_valid(formsets) and form_validated:
     777                self.save_model(request, new_object, form, change=False)
     778                form.save_m2m()
    781779                for formset in formsets:
    782780                    self.save_formset(request, form, formset, change=False)
    783781
    784782                self.log_addition(request, new_object)
    785783                return self.response_add(request, new_object)
    786             elif form_validated:
    787                 # The form was valid, but formsets were not, so delete the
    788                 # object we saved above.
    789                 new_object.delete()
    790784        else:
    791785            # Prepare the dict of initial data from the request.
    792786            # We have to special-case M2Ms as a list of comma-separated PKs.
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 1203233..ac30e42 100644
    a b class BaseModelForm(BaseForm): 
    254254            model_fields = set([f.name for f in self.instance._meta.fields])
    255255            form_fields = set(self.fields)
    256256            excluded_fields = model_fields - form_fields
     257            # HACK: Don't validate InlineForeignKeyField. If the parent model
     258            # hasn't been saved, it won't have a value and will fail model
     259            # field validation. See #12507 for details.
     260            for fname in self.fields:
     261                if isinstance(self.fields[fname], InlineForeignKeyField):
     262                    excluded_fields.add(fname)
    257263            failed_fields = set(self._errors.keys())
    258264            self.instance.full_validate(exclude=failed_fields.union(excluded_fields))
    259265        except ValidationError, e:
Back to Top