Opened 12 years ago

Closed 12 years ago

#18470 closed Cleanup/optimization (invalid)

Widget argument description in Form fields documentation

Reported by: jnschaeffer@… Owned by: nobody
Component: Documentation Version: 1.4
Severity: Normal Keywords:
Cc: Craig Blaszczyk Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

In the description of the widget field in form fields, the specified type for the parameter is "a Widget class". However, passing the class in throws an error--instead, it seems that the user must pass in an instance of the Widget class. Fixing this problem doesn't take much time, but it does take a bit of guesswork and reading between the lines of the documentation rather than simply going by what it says.

Attachments (1)

forms.py (731 bytes ) - added by jnschaeffer@… 12 years ago.
Pair of example fields, one using a class-based widget and one using an object-based widget

Download all attachments as: .zip

Change History (5)

comment:1 by Craig Blaszczyk, 12 years ago

Cc: Craig Blaszczyk added

Can you show some example code which shows the error? The form field should accept either an instance or a class

by jnschaeffer@…, 12 years ago

Attachment: forms.py added

Pair of example fields, one using a class-based widget and one using an object-based widget

comment:2 by jnschaeffer@…, 12 years ago

Added a forms.py with two fields (FieldWithClassWidget and FieldWithObjectWidget) and two forms (FormWithClassWidget and FormWithObjectWidget). When FormWithClassWidget is rendered, Django raises an AttributeError with the message "type object 'Textarea' has no attribute 'attrs'". When FormWithObjectWidget is rendered, the field displays correctly.

comment:3 by John Schaeffer <jnschaeffer@…>, 12 years ago

Type: BugCleanup/optimization

comment:4 by Karen Tracey, 12 years ago

Resolution: invalid
Status: newclosed

A form field does take either an instance or a class for widget, as is shown in doc here: https://docs.djangoproject.com/en/1.4/ref/forms/widgets/#setting-arguments-for-widgets. In that example birth_year gets an instance and favorite_colors gets a class. This works. That's not what your example code is doing. In your example code you are setting the widget value inside __init__ after calling the superclass __init__. It is the superclass __init__ that will take the class and turn it into an instance, so after calling superclass __init__, if you want to further change the field's widget value, you must specify an instance. I don't see any documentation that needs to be corrected here, if in fact we have an example that corresponds to what the sample code is doing then please point to it explicitly for correction.

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