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

16👍

Use TypedChoiceField.

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

6👍

field = BooleanField(widget=RadioSelect(choices=YES_OR_NO), required=False)


YES_OR_NO = (
    (True, 'Yes'),
    (False, 'No')
)
👤mpen

2👍

Use this if you want the horizontal renderer.

http://djangosnippets.org/snippets/1956/

👤Coc B.

2👍

If you want to deal with boolean values instead of integer values then this is the way to do it.

forms.TypedChoiceField(
    choices=((True, 'Yes'), (False, 'No')),
    widget=forms.RadioSelect,
    coerce=lambda x: x == 'True'
)
👤jhrr

Leave a comment