phunehehe
phunehehe

Reputation: 8778

Disable the empty option in Django ModelForm

I use widget=forms.CheckboxSelectMultiple in a ModelForm subclass to change the field display to checkboxes. The choices do become checkboxes but Django adds a first "empty" option with --------- as the content.

This behavior is OK when displaying the field using the Select widget, because it force the user to explicitly choose an option. However when using checkboxes it becomes useless and rather confusing. How can I disable it?

Upvotes: 3

Views: 2862

Answers (1)

phunehehe
phunehehe

Reputation: 8778

Err my bad, I should have looked carefully at the docs. Anyway here is the solution:

By default the widget used by ModelChoiceField will have a an empty choice at the top of the list. You can change the text of this label (which is "---------" by default) with the empty_label attribute, or you can disable the empty label entirely by setting empty_label to None

Upvotes: 4

Related Questions