Jibin
Jibin

Reputation: 3102

Django dynamic ChoiceField and validation error

I have a field in my form

myFormList = [(u'Select',u'Select')]
myForm  = forms.ChoiceField(choices=myFormList)

and I am initializing it dynamically using

form.fields['myForm'].choices =  form.fields['myForm'].choices + anotherMyFormList

This is causing a validation error Select a valid choice. ** is not one of the available choices

Usually I get rid of this error by

myForm  = forms.CharField(widget = forms.Select(choices=myFormList))   

But since I have to use form.fields['myForm'].choices while dynamically initializing I cannot use widgets.How do I do this form.fields['myForm'].widgets.choices =

Upvotes: 0

Views: 2375

Answers (1)

Jibin
Jibin

Reputation: 3102

Python is superb.

form.fields['myForm'].widgets.choices = was actually my pseudo-code & that worked as such.

Upvotes: 3

Related Questions