Opened 17 years ago
Closed 17 years ago
#5033 closed (wontfix)
Dynamic initial values for MultipleChoiceFields in newforms
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Documentation | Version: | dev |
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: | no | UI/UX: | no |
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.
Documenting all of the possible types of initial data to pass into different form fields probably isn't practical.