mirror of
https://github.com/django/django.git
synced 2025-10-25 06:36:07 +00:00
Fixed #24706 -- Made ModelForm._post_clean() handle a ValidationError raised when constructing the model instance.
Thanks Loïc Bistuer for review and advice.
This commit is contained in:
@@ -347,7 +347,15 @@ class BaseModelForm(BaseForm):
|
||||
# Override any validation error messages defined at the model level
|
||||
# with those defined at the form level.
|
||||
opts = self._meta
|
||||
for field, messages in errors.error_dict.items():
|
||||
|
||||
# Allow the model generated by construct_instance() to raise
|
||||
# ValidationError and have them handled in the same way as others.
|
||||
if hasattr(errors, 'error_dict'):
|
||||
error_dict = errors.error_dict
|
||||
else:
|
||||
error_dict = {NON_FIELD_ERRORS: errors}
|
||||
|
||||
for field, messages in error_dict.items():
|
||||
if (field == NON_FIELD_ERRORS and opts.error_messages and
|
||||
NON_FIELD_ERRORS in opts.error_messages):
|
||||
error_messages = opts.error_messages[NON_FIELD_ERRORS]
|
||||
@@ -379,8 +387,10 @@ class BaseModelForm(BaseForm):
|
||||
if isinstance(field, InlineForeignKeyField):
|
||||
exclude.append(name)
|
||||
|
||||
# Update the model instance with self.cleaned_data.
|
||||
self.instance = construct_instance(self, self.instance, opts.fields, exclude)
|
||||
try:
|
||||
self.instance = construct_instance(self, self.instance, opts.fields, exclude)
|
||||
except ValidationError as e:
|
||||
self._update_errors(e)
|
||||
|
||||
try:
|
||||
self.instance.full_clean(exclude=exclude, validate_unique=False)
|
||||
|
||||
Reference in New Issue
Block a user