Field.formfield() skips show_hidden_input parameter
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)
Fixed by r12696.