Disable empty option in Django ModelForm

I use widget=forms.CheckboxSelectMultiplein a subclass ModelFormto change the display of a field to checkboxes. The selection becomes a check mark, but Django adds the first “empty” option with ---------as content.

When a field is displayed using a widget, Selectthis behavior is OK, because it forces the user to explicitly select a parameter. However, when using checkboxes it becomes useless and rather confusing. How to disable it?

+3
source share
1 answer

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

, ModelChoiceField, . ( - "---------" ) empty_label, , empty_label None

+3

Source: https://habr.com/ru/post/1781937/


All Articles