Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#12913 closed (fixed)

Field.formfield() skips show_hidden_input parameter

Reported by: Ilya 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: no UI/UX: no


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/
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',
                    del kwargs[k]
        return form_class(**defaults)

# django/forms/
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 Ilya Semenov 9 years ago.

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by Ilya Semenov

comment:1 Changed 9 years ago by Ilya Semenov

Needs tests: set

comment:2 Changed 9 years ago by Russell Keith-Magee

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:3 Changed 9 years ago by jkocherhans

Resolution: fixed
Status: newclosed

Fixed by r12696.

comment:4 Changed 9 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 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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