1
0
mirror of https://github.com/django/django.git synced 2025-10-30 00:56:09 +00:00

Fixed #13776 -- Fixed ModelForm.is_valid() exception with non-nullable FK and blank=True.

Thanks peterbe for the report.
This commit is contained in:
Anubhav Joshi
2014-06-03 09:06:59 +05:30
committed by Tim Graham
parent 7e2c87809c
commit 45e049937d
3 changed files with 44 additions and 3 deletions

View File

@@ -401,10 +401,12 @@ class BaseModelForm(BaseForm):
def _post_clean(self):
opts = self._meta
# Update the model instance with self.cleaned_data.
self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
exclude = self._get_validation_exclusions()
# a subset of `exclude` which won't have the InlineForeignKeyField
# if we're adding a new object since that value doesn't exist
# until after the new instance is saved to the database.
construct_instance_exclude = list(exclude)
# Foreign Keys being used to represent inline relationships
# are excluded from basic field value validation. This is for two
@@ -415,8 +417,13 @@ class BaseModelForm(BaseForm):
# so this can't be part of _get_validation_exclusions().
for name, field in self.fields.items():
if isinstance(field, InlineForeignKeyField):
if self.cleaned_data.get(name) is not None and self.cleaned_data[name]._state.adding:
construct_instance_exclude.append(name)
exclude.append(name)
# Update the model instance with self.cleaned_data.
self.instance = construct_instance(self, self.instance, opts.fields, construct_instance_exclude)
try:
self.instance.full_clean(exclude=exclude, validate_unique=False)
except ValidationError as e: