Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - CheckboxSelectMultiple without "------" choice

How can I remove "------" from rendered choices? I use in my model form:

widgets = {
    'event_form': forms.CheckboxSelectMultiple(),
}

In model I have IntegerField with choices:

EVENT_FORM_CHOICES = (
    (1, _(u'aaaa')),
    (2, _(u'bbbb')),
    (3, _(cccc')),
    (4, _(u'dddd')),
    (5, _(eeee'))
)

rendered choices contain --------- as first possible choice. How I can get rid of it?

EDIT: The only working way i figured out is (in init method):

tmp_choices = self.fields['event_form'].choices
del tmp_choices[0]
self.fields['event_form'].choices = tmp_choices

but it's not very elegant way :)

like image 681
tunarob Avatar asked Oct 08 '22 13:10

tunarob


1 Answers

Update

a similar example maybe useful:

country = ModelChoiceField(reference_class = Country, choices= country_choices, 
required=True, empty_label=None,  widget=forms.Select)

If you want a solution client side instead:

<script>     
$("#selectBox option[value='-----']").remove(); 
</script>
like image 197
asdf_enel_hak Avatar answered Oct 12 '22 09:10

asdf_enel_hak