Opened 8 years ago

Closed 8 years ago

#5033 closed (wontfix)

Dynamic initial values for MultipleChoiceFields in newforms

Reported by: matt@… Owned by: nobody
Component: Documentation Version: master
Severity: Keywords: dynamic values, MultipleChoiceField, newforms
Cc: matt@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The newforms documentation specifies that forms can be pre-populated with dynamic values using an 'initial' dictionary. From the docs:

>>> class CommentForm(forms.Form):
...     name = forms.CharField()
...     url = forms.URLField()
...     comment = forms.CharField()
>>> f = CommentForm(initial={'name': 'your username'}, auto_id=False)
>>> print f
<tr><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr>
...

This works fine if the form element is a CharField as in the example above. However, if the form element is a MultipleChoiceField, the initial value is required to be the id of the object you're trying to select - not the string representation as required for CharField, TextField et al.

It's fairly obvious when you think about it, but it would have saved me some time if the docs mentioned it.

Change History (1)

comment:1 Changed 8 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

Documenting all of the possible types of initial data to pass into different form fields probably isn't practical.

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