Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#25431 closed Bug (fixed)

ModelFormset regression in object clean() accessing a FK

Reported by: Claude Paroz Owned by: nobody
Component: Forms Version: 1.8
Severity: Release blocker Keywords: regression
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When a ModelForm with an inline formset is validated (at least when all objects are new), the inline instances are constructed without the link to their parent model, because the fk field is excluded from the instance construction (see BaseModelForm._post_clean).

Like #25349, this regression is probably caused by 45e049937d2564d11035827ca9a9221b86215e45/#13776.

Change History (6)

comment:1 Changed 4 years ago by Claude Paroz

Just apply that patch to reproduce the problem in Django's test suite:

  • tests/model_formsets/models.py

    diff --git a/tests/model_formsets/models.py b/tests/model_formsets/models.py
    index df6a687..7b7337f 100644
    a b class Book(models.Model): 
    3737    def __str__(self):
    3838        return self.title
    3939
     40    def clean(self):
     41        # Ensure author is always accessible in clean method
     42        assert self.author.name is not None
     43
    4044
    4145@python_2_unicode_compatible
    4246class BookWithCustomPK(models.Model):

comment:2 Changed 4 years ago by Claude Paroz

Has patch: set

If the patch gets approved, I'd like to tentatively backport this to 1.8.

comment:3 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:4 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:5 Changed 4 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In 65a1055:

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.

comment:6 Changed 4 years ago by Claude Paroz <claude@…>

In 158b0a28:

[1.8.x] 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.

Backport of 65a1055a3 from master.

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