Default Choice in Choice Field (Django)

1.2k Views Asked by At

I am trying to create dependent dropdowns following this link: http://www.devinterface.com/blog/en/2011/02/how-to-implement-two-dropdowns-dependent-on-each-other-using-django-and-jquery/

But I have this form:

class FrequencyForm(CityForm)
    def __init__(self, *args, **kwargs):
        super(FrequencyForm, self).__init__(*args, **kwargs)
        self.frequency_list = [('-1','None')]
        self.fields['term'] = forms.ChoiceField(choices=self.frequency_list, required=True, widget=forms.RadioSelect())

I don't want the default value to appear when the list populates. What should I do?

1

There are 1 best solutions below

0
On

In

self.fields['term'] = forms.ChoiceField(choices=self.frequency_list, required=True, widget=forms.RadioSelect())

self.frequency_list is a list of tuples. As far as I can see there is only one choice for your dropdown i.e [('-1','None')]. Try putting an entry at first index in this list, for eg.

[('','None'),('-1','None')]

and now the first one will become the default selected element.

For more you can refer this : Setting the selected value on a Django forms.ChoiceField