diff options
| author | Claude Paroz <claude@2xlibre.net> | 2015-09-19 14:23:55 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2015-09-21 21:30:49 +0200 |
| commit | 65a1055a36318ff4e21ffeb7c3dd62fa81892269 (patch) | |
| tree | 898d79ec9b34d4b6693b3830c1b221696eed9222 | |
| parent | c07f9fef398a21a76d350e8b02b396b3e580b751 (diff) | |
Fixed #25431 -- Readded inline foreign keys to modelformset instances
Too much field exclusions in form's construct_instance() in _post_clean()
could lead to some unexpected missing ForeignKey values.
Fixes a regression from 45e049937. Refs #13776.
| -rw-r--r-- | django/forms/models.py | 10 | ||||
| -rw-r--r-- | docs/releases/1.8.5.txt | 3 | ||||
| -rw-r--r-- | tests/model_formsets/models.py | 4 |
3 files changed, 12 insertions, 5 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index 0fba08855f..ac8bd1c860 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -376,6 +376,11 @@ class BaseModelForm(BaseForm): exclude = self._get_validation_exclusions() + try: + self.instance = construct_instance(self, self.instance, opts.fields, exclude) + except ValidationError as e: + self._update_errors(e) + # Foreign Keys being used to represent inline relationships # are excluded from basic field value validation. This is for two # reasons: firstly, the value may not be supplied (#12507; the @@ -388,11 +393,6 @@ class BaseModelForm(BaseForm): exclude.append(name) 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) except ValidationError as e: self._update_errors(e) diff --git a/docs/releases/1.8.5.txt b/docs/releases/1.8.5.txt index ce03577cde..9cdd802057 100644 --- a/docs/releases/1.8.5.txt +++ b/docs/releases/1.8.5.txt @@ -43,3 +43,6 @@ Bugfixes * Moved the :ref:`unsaved model instance assignment data loss check <unsaved-model-instance-check-18>` on reverse relations to ``Model.save()`` (:ticket:`25160`). + +* Readded inline foreign keys to form instances when validating model formsets + (:ticket:`25431`). diff --git a/tests/model_formsets/models.py b/tests/model_formsets/models.py index df6a68792b..7b7337fd52 100644 --- a/tests/model_formsets/models.py +++ b/tests/model_formsets/models.py @@ -37,6 +37,10 @@ class Book(models.Model): def __str__(self): return self.title + def clean(self): + # Ensure author is always accessible in clean method + assert self.author.name is not None + @python_2_unicode_compatible class BookWithCustomPK(models.Model): |
