Handle error form.save() with model validation

Handle error form.save() with model validation

Here is a quick snippet to avoid form.save() error because model validation :

1
2
3
4
5
6
7
8
# handle form save with model validationerror
# http://stackoverflow.com/questions/8771029/django-raise-a-validation-error-in-a-models-save-method
try:
    form.save()
except ValidationError, e:
    form._errors = {}
    for _, v in e.message_dict.items():
        form._errors.setdefault(NON_FIELD_ERRORS, []).extend(v)

Leave a Reply

Your email address will not be published. Required fields are marked *