Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#12913 closed (fixed)

Field.formfield() skips show_hidden_input parameter

Reported by: Ilya Semenov Owned by: nobody
Component: Forms Version: dev
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

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 Ilya Semenov 15 years ago.

Download all attachments as: .zip

Change History (6)

by Ilya Semenov, 15 years ago

comment:1 by Ilya Semenov, 15 years ago

Needs tests: set

comment:2 by Russell Keith-Magee, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:3 by jkocherhans, 15 years ago

Resolution: fixed
Status: newclosed

Fixed by r12696.

comment:4 by jkocherhans, 15 years ago

(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 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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