Code

Opened 5 years ago

Closed 3 years ago

#11418 closed (fixed)

formset.cleaned_data raises AttributeError when is_valid is True

Reported by: adurdin Owned by: nobody
Component: Forms Version: master
Severity: Keywords: sprintSep2010
Cc: me@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

With a formset with no initial forms and empty extra forms, formset.is_valid() returns True, but formset.cleaned_data raises an AttributeError.

This can be found with the examples from the formsets docs:

>>> from django import forms
>>> class ArticleForm(forms.Form):
...     title = forms.CharField()
...     pub_date = forms.DateField()
... 
>>> from django.forms.formsets import formset_factory
>>> ArticleFormSet = formset_factory(ArticleForm)
>>> formset = ArticleFormSet({})
>>> 
>>> formset.is_valid()
True
>>> formset.errors
[{}]
>>> formset.cleaned_data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/andy/dev/django/dj/django/forms/formsets.py", line 128, in _get_cleaned_data
    return [form.cleaned_data for form in self.forms]
AttributeError: 'ArticleForm' object has no attribute 'cleaned_data'

Attachments (2)

formset.diff (2.2 KB) - added by mlavin 4 years ago.
Change to BaseFormSet._construct_form with tests
11418.patch (5.8 KB) - added by mlavin 4 years ago.
New patch vs [13738] with doc updates

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by mlavin

  • Owner changed from nobody to mlavin
  • Status changed from new to assigned

This formset does have a form in it and it is not valid. The formset itself is bound but the form is not bound (and hence not valid).

>>> from django import forms
>>> class ArticleForm(forms.Form):
...     title = forms.CharField()
...     pub_date = forms.DateField()
... 
>>> from django.forms.formsets import formset_factory
>>> ArticleFormSet = formset_factory(ArticleForm)
>>> formset = ArticleFormSet({})
>>> formset.forms
[<__main__.ArticleForm object at 0x94636cc>]
>>> formset.forms[0].is_valid()
False
>>> formset.is_bound
True
>>> formset.forms[0].is_bound
False

I think the problem is in the BaseFormSet._construct_form:

    def _construct_form(self, i, **kwargs):
        """
        Instantiates and returns the i-th form instance in a formset.
        """
        defaults = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)}
        if self.data or self.files:
            defaults['data'] = self.data
            defaults['files'] = self.files
...

if self.data or self.files: should be if self.is_bound: because the empty data dictionary is not being passed down to the forms.

Changed 4 years ago by mlavin

Change to BaseFormSet._construct_form with tests

comment:3 Changed 4 years ago by mlavin

  • Has patch set

comment:4 Changed 4 years ago by mlavin

  • Owner changed from mlavin to kmtracey
  • Status changed from assigned to new

comment:5 Changed 4 years ago by mlavin

  • Owner changed from kmtracey to mlavin
  • Patch needs improvement set

After coming back to this and speaking with kmtracy I'm not sure the described functionality is correct. A valid formset should have cleaned_data attribute but a formset passed an empty dict should raise a ValidationError "ManagementForm data is missing or has been tampered with". The formset validation needs some work but I believe the docs need an update as well.

Changed 4 years ago by mlavin

New patch vs [13738] with doc updates

comment:6 Changed 4 years ago by mlavin

  • Owner changed from mlavin to kmtracey
  • Patch needs improvement unset

New patch which updates the validation logic and the docs around formsets. This does introduce a small backwards incompatible change in that previously passing an empty dict would validate but now it will throw and exception that the Management Form is invalid which I believe is the more intuitive behavior. Let me know if you think it needs some more work.

comment:7 Changed 4 years ago by kmtracey

  • Owner changed from kmtracey to nobody

Removing myself as owner since I'm not going to be able to finish this tonight and then I'll be gone, so if someone else wants to take a look feel free.

comment:8 Changed 4 years ago by mlavin

  • Keywords sprintSep2010 added
  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 3 years ago by Honza_Kral

  • Resolution set to fixed
  • Status changed from new to closed

(In [14667]) Fixed #11418 -- formset.cleaned_data no longer raises AttributeError when is_valid is True. Thanks mlavin!

This also introduces a slightly backwards-incompatible change in
FormSet's behavior, see the release docs for details.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.