Django BooleanField as radio buttons

django, django-forms, django-models

Is there a widget in Django 1.0.2 to render a models.BooleanField as two radio buttons instead of a checkbox?

Best Solution

Django 1.2 has added the "widgets" Meta option for modelforms:

In your models.py, specify the "choices" for your boolean field:

BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))class MyModel(models.Model):    yes_or_no = models.BooleanField(choices=BOOL_CHOICES)

Then, in your forms.py, specify the RadioSelect widget for that field:

class MyModelForm(forms.ModelForm):    class Meta:        model = MyModel        widgets = {            'yes_or_no': forms.RadioSelect        }

I've tested this with a SQLite db, which also stores booleans as 1/0 values, and it seems to work fine without a custom coerce function.