Display a boolean model field in a django form as a radio button rather than the default Checkbox

前端 未结 4 1913
猫巷女王i
猫巷女王i 2021-02-08 06:19

This is how I went about, to display a Boolean model field in the form as Radio buttons Yes and No.

choices = ( (1,\'Yes\'),
            (0,\'No\'),
          )
         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 06:26

    Use TypedChoiceField.

    class EmailEditForm(forms.ModelForm):
        to_send_form = forms.TypedChoiceField(
                             choices=choices, widget=forms.RadioSelect, coerce=int
                        )
    

提交回复
热议问题