Code

Opened 4 years ago

Closed 4 years ago

#14119 closed (fixed)

fields_for_model returns all fields when fields parameter is the empty tuple

Reported by: alexdutton Owned by: nobody
Component: Forms Version: 1.2
Severity: Keywords: model forms
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When attempting to use modelformset_factory to generate forms where the user can only delete objects (i.e. not edit them), one would expect a fields=() to create forms with only delete checkboxes. However, fields_for_model uses the following check to determine whether to restrict fields:

 if fields and not f.name in fields:
     continue

When passed the empty tuple, fields are never skipped as bool( () ) == False, resulting in all fields being displayed.

This should be:

 if fields is not None and not f.name in fields:
     continue

I've attached a patch for both construct_instance (which also has the wrong behaviour) and fields_for_model.

The additional regression tests cover construct_instance, fields_for_model, modelformset_factory and ModelForm. The latter is included even though the usage of fields=() in the this case is arguably nonsensical.

Attachments (1)

fix-14119.diff (3.7 KB) - added by alexdutton 4 years ago.
Patch and regression tests

Download all attachments as: .zip

Change History (3)

Changed 4 years ago by alexdutton

Patch and regression tests

comment:1 Changed 4 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:2 Changed 4 years ago by Honza_Kral

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

(In [14199]) Fixed #14119 -- fields_for_model no longer returns all fields when fields parameter is the empty tuple. Thanks alexdutton!

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.