I have the following django form:
class SpecifyColumnsForm(forms.Form):
columns = forms.MultipleChoiceField(required=False,
widget=forms.CheckboxSelectMultiple)
Now, I want to specify the choices for this MultipleChoiceField
from views.py
. How can I do that?
I have tried the following, but it did not work:
columns_form = SpecifyColumnsForm(request.POST)
columns_form.choices = (('somestuff', 'spam'),
('otherstuff', 'eggs'),
('banana', 'bar'))
Thanks!
The documentation itself states that
So all you have to do is
In your views.py you have to set:
This works for me, I'm just not sure if you can avoid to not set
choices
in forms.py. I think you need anyway to put it in forms.py, if this is a required argument forMultipleChoiceField
otherwise your form might not be valid.