Opened 8 years ago

Closed 8 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


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) (731 bytes) - added by jnschaeffer@… 8 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 Changed 8 years ago by Craig Blaszczyk

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

Changed 8 years ago by jnschaeffer@…

Attachment: added

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

comment:2 Changed 8 years ago by jnschaeffer@…

Added a 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 Changed 8 years ago by John Schaeffer <jnschaeffer@…>

Type: BugCleanup/optimization

comment:4 Changed 8 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

A form field does take either an instance or a class for widget, as is shown in doc here: 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