form with CheckboxSelectMultiple doesn't validate

前端 未结 2 2011
清歌不尽
清歌不尽 2021-02-05 07:10

I have a form with a choice field that is using CheckboxSelectMultiple widget:

foo = forms.ChoiceField(widget=forms.CheckboxSelectMultiple,
                              


        
相关标签:
2条回答
  • 2021-02-05 07:19

    May this helpful for you

    num_choices = ( ("1", "ONE"), ("2", "TWO"), ("3", "Three"), ("4", "Four"))
    
    num_list = forms.MultipleChoiceField(choices=num_choices, required=True, widget=forms.CheckboxSelectMultiple(), label='Select No', initial=("1", "2"))
    

    If you want to pass the ORM object directly, then you can try the following

    num_list = forms.ModelMultipleChoiceField(Numbers.objects.all(), required=True, widget=forms.CheckboxSelectMultiple(), label='Select No')
    
    0 讨论(0)
  • If you make the field a forms.MultipleChoiceField rather than a forms.ChoiceField it will work better.

    0 讨论(0)
提交回复
热议问题