Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12913 closed (fixed)

Field.formfield() skips show_hidden_input parameter

Reported by: semenov Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

The base models.Field.formfield() implementation silently skips show_hidden_input parameter for any Field with choices. That is incorrect, since show_hidden_input is a base parameter accepted by any form.Field, including TypedChoiceField:

# django/db/models/fields/__init__.py
class Field(object):
    # ...
    def formfield(self, form_class=forms.CharField, **kwargs):
        # ...
        if self.choices:
            # Many of the subclass-specific formfield arguments (min_value,
            # max_value) don't apply for choice fields, so be sure to only pass
            # the values that TypedChoiceField will understand.
            for k in kwargs.keys():
                if k not in ('coerce', 'empty_value', 'choices', 'required',
                             'widget', 'label', 'initial', 'help_text',
                             'error_messages'):
                    del kwargs[k]
        defaults.update(kwargs)
        return form_class(**defaults)

# django/forms/fields.py
class Field(object):
    # ...
    def __init__(self, required=True, widget=None, label=None, initial=None,
                 help_text=None, error_messages=None, show_hidden_initial=False):

That forces a user to use ugly constructions like:

def _formfield_with_hidden_initial(f):
        field = f.formfield()
        field.show_hidden_initial = True
        return field

class SimultaneousEditForm(forms.ModelForm):
        formfield_callback = _formfield_with_hidden_initial

With the proposed fix, the code above could be far more elegant (exactly as it supposed to be by model.Field.formfield() authors!) while not breaking anything:

class SimultaneousEditForm(forms.ModelForm):
        formfield_callback = lambda f: f.formfield(show_hidden_initial=True)

Attachments (1)

formfield_not_skipping_hidden_initial.diff (657 bytes) - added by semenov 4 years ago.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by semenov

comment:1 Changed 4 years ago by semenov

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

comment:2 Changed 4 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 4 years ago by jkocherhans

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

Fixed by r12696.

comment:4 Changed 4 years ago by jkocherhans

(In [12697]) [1.1.X] Fixed #12913. Fields with choices now respect show_hidden_initial as a keyword argument to formfield. Backport of r12696 from trunk.

comment:5 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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.