Setting the selected value on a Django forms.ChoiceField

thebiglife picture thebiglife · Mar 18, 2009 · Viewed 144k times · Source

Here is the field declaration in a form:

max_number = forms.ChoiceField(widget = forms.Select(), 
    choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,)

I would like to set the initial value to be 3 and this doesn't seem to work. I have played about with the param, quotes/no quotes, etc... but no change.

Could anyone give me a definitive answer if it is possible? And/or the necessary tweak in my code snippet?

I am using Django 1.0

Answer

Tom picture Tom · Mar 20, 2009

Try setting the initial value when you instantiate the form:

form = MyForm(initial={'max_number': '3'})