Lakerolis
Lakerolis

Reputation: 17

How to customize the default <option> in a selector

I have created the following form:

class ProductFilter(forms.Form):
    price = forms.IntegerField(label='Price', widget=forms.Select(choices=PRICE_CHOICES), required=False)
    category = forms.ModelChoiceField(queryset=Category.objects.all(), required=False)
    language = forms.ModelChoiceField(queryset=ProductLanguage.objects.all(), required=False)

The default <option> in both category and language is displayed as "------------". I know i can set initial="...", but I would like the default option to be something that is not part of the queryset.

Is it possibe to insert an option somehow? I don't want to create a language or a category called "all".

Upvotes: 1

Views: 31

Answers (1)

Alasdair
Alasdair

Reputation: 308879

You can change the ------------ by setting empty_label.

category = forms.ModelChoiceField(queryset=Category.objects.all(), required=False, empty_label='(none)')

Note that this value is used when no category is selected. It would be confusing to set empty_label="all".

Upvotes: 1

Related Questions