I have a basic form
class AssumptionsForm(forms.Form):
writedownoper = forms.ChoiceField( required=True, choices =
[('1','1'),('2',2='),('3','3')])
Now in my view after I create the form I want to set value 2 as
default. How can I do it? Is there set default API that I can use.
Thanks in advance
Sumanth
There are two ways you can do this. If the initial choice for
'writedownoper' will always be '2', you can use pass an 'initial'
argument when declaring the field [1]:
writedownoper = forms.ChoiceField( required=True, choices =
[('1','1'),('2','2'),('3','3')], initial = '2')
However, if that value needs to be dynamic, you can do so when
instantiating the form in your view [2]:
writedownoper = forms.ChoiceField( required=True, choices =
[('1','1'),('2','2'),('3','3')])
views.py:
form = AssumptionsForm(initial = {'writedownopen' : '2'})
>
> Thanks in advance
> Sumanth
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
--
Best,
R
[1] - http://docs.djangoproject.com/en/dev/ref/forms/fields/#initial
[2] - http://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values
form = SomeForm()
form.fields['meal_pref'].initial = 2
Shawn
Sumanth