Is there a way to make a Django BooleanField a drop down in a form?
Right now it renders as a radio button. Is it possible to have a dropdown with options: 'Yes', 'No' ?
Currently my form definition for this field is:
attending = forms.BooleanField(required=True)
What you can do is add "choices" key word to your BooleanField in your models.py
class MyModel(models.Model):
BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))
attending = models.BooleanField(choices=BOOL_CHOICES)
I believe a solution that can solve your problem is something along the lines of this:
TRUE_FALSE_CHOICES = (
(True, 'Yes'),
(False, 'No')
)
boolfield = forms.ChoiceField(choices = TRUE_FALSE_CHOICES, label="Some Label",
initial='', widget=forms.Select(), required=True)
Might not be exact but it should get you pointed in the right direction.
With a modelform
TRUE_FALSE_CHOICES = (
(True, 'Yes'),
(False, 'No')
)
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('attending',)
widgets = {
'attending': forms.Select(choices=TRUE_FALSE_CHOICES)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With