Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django BooleanField as radio buttons?

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

like image 221
dar Avatar asked May 12 '09 20:05

dar


1 Answers

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.

like image 163
eternicode Avatar answered Sep 20 '22 10:09

eternicode