Empty Label ChoiceField Django
You can try this (assuming your choices are tuples):
blank_choice = (('', '---------'),)
...
color = forms.ChoiceField(choices=blank_choice + COLORS)
year = forms.ChoiceField(choices=blank_choice + YEAR_CHOICES)
Also, I can't tell from your code whether this is a form or a ModelForm, but it it's the latter, no need to redefine the form field here (you can include the choices=COLORS and choices=YEAR_CHOICES directly in the model field.
Hope this helps.
I know you already accepted an answer but I just want to post this in case someone out there runs into the issue I was having, namely the accepted solution does not work with a ValueListQuerySet. The EmptyChoiceField, which you linked to, works perfectly for me (although I am using django 1.7).
class EmptyChoiceField(forms.ChoiceField):
def __init__(self, choices=(), empty_label=None, required=True, widget=None, label=None, initial=None, help_text=None, *args, **kwargs):
# prepend an empty label if it exists (and field is not required!)
if not required and empty_label is not None:
choices = tuple([(u'', empty_label)] + list(choices))
super(EmptyChoiceField, self).__init__(choices=choices, required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs)
class FilterForm(forms.ModelForm):
#place your other fields here
state = EmptyChoiceField(choices=People.objects.all().values_list("state", "state").distinct(), required=False, empty_label="Show All")
Here's the solution that I used:
from myapp.models import COLORS
COLORS_EMPTY = [('','---------')] + COLORS
class ColorBrowseForm(forms.Form):
color = forms.ChoiceField(choices=COLORS_EMPTY, required=False, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
See the Django 1.11 documentation on ChoiceField. The 'empty value' for the ChoiceField is defined as the empty string ''
, so your list of tuples should contain a key of ''
mapped to whatever value you want to show for the empty value.
### forms.py
from django.forms import Form, ChoiceField
CHOICE_LIST = [
('', '----'), # replace the value '----' with whatever you want, it won't matter
(1, 'Rock'),
(2, 'Hard Place')
]
class SomeForm (Form):
some_choice = ChoiceField(choices=CHOICE_LIST, required=False)
Note, you can avoid a form error if you want the form field to be optional by using required=False
Also, if you already have a CHOICE_LIST without an empty value, you can insert one so it shows up first in the form drop-down menu:
CHOICE_LIST.insert(0, ('', '----'))