Opened 8 years ago

Closed 6 years ago

#16423 closed Cleanup/optimization (fixed)

ModelForm._post_clean() does not respect Model.clean() ValidationErrors that possess a message_dict, rather than a list of messages

Reported by: robboyle Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords: modelform, validation
Cc: chris+django@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When a ModelForm runs it's model-level validation, it anticipates the Model's clean_fields() method raising a ValidationError with the message_dict property set. This let's the ModelForm associate the Model's Field validation errors with the ModelForm fields.

However, when the ModelForm then calls the Model's clean() method, it ignores the message_dict property if the ValidationError raised by clean() has it set. Instead, it always creates a new dictionary, associating all validation messages with the NON_FIELD_ERRORS key.

It would be nice if _post_clean() checked if Model.clean() was raising a ValidationError with a message_dict, and if so updated the errors with that dict, rather than blindly creating a new one. That way, the Model.clean() method could associate errors with a particular field, return a ValidationError with message_dict set, and this association would be perserved by the ModelForm.

Being able to have a form's clean method associate errors with a particular field is very handy. Being able to do this at the model level and then have model forms take advantage of it would bring model forms more on par with regular forms.

Attachments (1)

16423.diff (618 bytes) - added by robboyle 8 years ago.
My initial shot at a solution.

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by robboyle

Attachment: 16423.diff added

My initial shot at a solution.

comment:1 Changed 8 years ago by Aymeric Augustin

Has patch: set
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 8 years ago by Jacob

Triage Stage: Design decision neededAccepted

comment:3 Changed 8 years ago by Chris Wilson

Cc: chris+django@… added


Model.full_clean does this, which is even simpler than the current patch:

        # Form.clean() is run even if other validation fails, so do the
        # same with Model.clean() for consistency.
        except ValidationError, e:
            errors = e.update_error_dict(errors)

You could do exactly the same in BaseModelForm.post_clean for symmetry:

        # Call the model instance's clean method.
        except ValidationError, e:

BaseModelForm._post_clean does almost the same thing as Model.full_clean, and could perhaps be simplified to just call it. The main difference seems to be that validating uniqueness is optional:

        # Validate uniqueness if needed.
        if self._validate_unique:

and would fail if self.instance.validate_unique threw a ValidationError with a NON_FIELD_ERROR, because it assumes that the ValidationError contains a message_dict:

        except ValidationError, e:

comment:4 Changed 6 years ago by Tim Graham

Resolution: fixed
Status: newclosed

I believe this was addressed by [f34cfec0].

Note: See TracTickets for help on using tickets.
Back to Top