Opened 11 years ago

Closed 7 days ago

Last modified 7 days ago

#20744 closed Bug (fixed)

Docs imply that forms.Field.__init__ accepts any keyword arguments

Reported by: Gunnlaugur Þór Briem Owned by: Adam Zapletal
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Pashkin, Adam Zapletal Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

At https://docs.djangoproject.com/en/dev/ref/forms/fields/ there is this:

class Field(**kwargs)

with no stated constraints on kwargs, thus implying (incorrectly) that any keyword argument can safely be passed to the forms.Field constructor.

That leads to code which worked in previous Django versions but regresses in the current version. The case I have in mind is in django-authority's admin.py where the kwargs passed to formfield_callback (which can include request) are passed on to Field.formfield, whose documentation says “All of the kwargs dictionary is passed directly to the form field’s __init__() method” ... and that __init__ method can't include request. So upgrading Django yields this somewhat tricky-to-troubleshoot error in a django-authority admin action:

TypeError: __init__() got an unexpected keyword argument 'request'

  ...
  File "django/forms/models.py", line 170, in fields_for_model
    formfield = formfield_callback(f, **kwargs)
  File "authority/admin.py", line 33, in formfield_for_dbfield
    return db_field.formfield(**kwargs)
  File "django/db/models/fields/__init__.py", line 646, in formfield
    return super(CharField, self).formfield(**defaults)
  File "django/db/models/fields/__init__.py", line 499, in formfield
    return form_class(**defaults)
  File "django/forms/fields.py", line 188, in __init__
    super(CharField, self).__init__(*args, **kwargs)

At some point in this chain, the request keyword argument must be popped out. Presumably directly before the call to Field.formfield. In any case, this requirement should be documented.

(Or forms.Field.__init__ could be changed to tolerate and ignore unknown keyword arguments. But that's a riskier change.)

Change History (6)

comment:1 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Pashkin, 10 years ago

Cc: Pashkin added

comment:3 by Adam Zapletal, 2 weeks ago

Cc: Adam Zapletal added
Has patch: set
Owner: changed from nobody to Adam Zapletal
Status: newassigned

I opened a simple pull request changing

.. class:: Field(**kwargs)

to

.. class:: Field

in the docs. That's how the Form class is documented.

comment:4 by Natalia Bidart, 7 days ago

Triage Stage: AcceptedReady for checkin

comment:5 by GitHub <noreply@…>, 7 days ago

Resolution: fixed
Status: assignedclosed

In 828b94b:

Fixed #20744 -- Removed hint that arbitrary kwargs are allowed when creating forms.Fields.

comment:6 by Natalia <124304+nessita@…>, 7 days ago

In f29922b:

[5.0.x] Fixed #20744 -- Removed hint that arbitrary kwargs are allowed when creating forms.Fields.

Backport of 828b94b178736f7882cc6e5cd86b5c8e84b62ece from main

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