Opened 3 years ago

Closed 3 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: jakul 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@… 3 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 3 years ago by jakul

  • Cc jakul added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

Changed 3 years ago by jnschaeffer@…

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

comment:2 Changed 3 years ago by jnschaeffer@…

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

  • Type changed from Bug to Cleanup/optimization

comment:4 Changed 3 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to closed

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