Search code examples
djangoformsdrop-down-menu

Set default value for dropdown in django forms


I am unable to set default value for a dropdown while loading forms.

Here is the code

state = forms.TypedChoiceField(choices = formfields.State)

State = (
         ('QC_APPROVED','QC_APPROVED'),
         ('REVERT','REVERT'),
         ('FIXED','FIXED'),
        )

If I want to make the default state as FIXED. I am writing this code

state = forms.TypedChoiceField(choices = formfields.State, default = 'FIXED')

If I execute the above code I am getting the below error.

Exception Value: __init__() got an unexpected keyword argument 'default'

Can some one help on this?


Solution

  • state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')
    

    As shown in documentation: http://docs.djangoproject.com/en/dev/ref/forms/fields/#initial