Cansada de ser feliz

Blog personal sobre libros, jardinería, historia, programación y otros temas que me interesan.

How to show a BooleanField of a ModelForm as radio select (yes/no) in Django

02 March 2015

Let’s suppose that we have a field is_active in our model. It is a boolean field, but in a template we want to show it as a radio select.

In this case we just need to add choices attribute for the model field and then change a widget of the corresponding form:

```python class MyForm(forms.ModelForm): class Meta: model = MyModel widgets = { ‘is_active’: forms.RadioSelect }

from django.db import models

class MyModel(models.Model):
        (True, u'Yes'),
        (False, u'No'),
    is_active = models.BooleanField(
        verbose_name=u'Is it active?',


For Django forms.


from django import forms

class MyForm(forms.Form): is_active = forms.TypedChoiceField( coerce=lambda x: bool(int(x)), choices=((0, ‘No’), (1, ‘Yes’)), widget=forms.RadioSelect ) ```