[Fixed]-Django BooleanField as a dropdown

33👍

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.

21👍

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)

5👍

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)
        }

Leave a comment