Suppose I created a django form that has a radio box selection in it:
class PlaylistsForm(forms.Form):
choices=forms.ChoiceField( widget=forms.RadioSelect(), CHOICES,label="choices")
If I try to instantiate the form and do form.choices I get an error that the instance has no attribute choices. Can you refer me to somewhere where it explains the magic behind the creation of the fields, and secondly, how can I access the fields?
EDIT:
To make it clear: I want to know why, given an instance of PlaylistsForm, doing
print form.choices I get an error saying there’s no such attribute. What dark magic is happening behind the scenes here?
Presuming
CHOICESis a tuple of options for the field, the change is relatively simple:See the documentation of
ChoiceField, and the documentation ofchoicesfor more detail.Changes post your edit:
Short answer
You’re probably confusing your field called
choices, and the attributechoiceson yourchoicesfield.Say you’ve got a form:
You could access the
choicesattribute of thechoicesfield like this:Long answer
I didn’t know how to do what you wanted, so I stuck a
import pdb; pdb.set_trace()just after I declared a form, like this:Then, using the development server, I opened up a URL which mapped to the view with my new
import pdb; pdb.set_trace()in it. Switching over to my command prompt, I could inspect what attributes and methods existed on my form object at the debug prompt:This showed me
formhad afieldsattribute, so I looked at it:This showed me
form.fieldsis adict, with values beingFieldobjects, I picked out thechoicesfield, and looked to see what attributes it had:This showed me that
form.fields['choices']had achoicesattribute:This is probably what you’re looking for.