I want my ChoiceField in ModelForm to have a blank option (------) but it's required.
I need to have blank option to prevent user from accidentally skipping the field thus select the wrong option.
This works for at least 1.4 and later:
CHOICES = (
('', '-----------'),
('foo', 'Foo')
)
class FooForm(forms.Form):
foo = forms.ChoiceField(choices=CHOICES)
Since ChoiceField is required (by default), it will complain about being empty when first choice is selected and wouldn't if second.
It's better to do it like this than the way Yuji Tomita showed, because this way you use Django's localized validation messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With