I'm trying to have a yes/no selection on a booleanfield. The default widget is checkboxinput. However if I override the default widget with Select I get a:
NameError: Select is not defined
I think this may be because I need to setup Yes/No to correlate to the boolean values in the booleanfield, but not sure how this should be done?
Model:
class User(models.Model):
online_account = models.BooleanField()
Form:
class AccountForm(forms.ModelForm):
class Meta:
model = User
fields = ('online_account')
labels = {
'online_account': 'Do you have an online account',
}
widgets = {'online_account': Select()}
I found (and tested with Django 1.9.6) this gist. It should do the trick:
from django import forms
class Form(forms.Form):
field = forms.TypedChoiceField(coerce=lambda x: x =='True',
choices=((False, 'No'), (True, 'Yes')))
Just set the choices in the template's boolean field
from django.utils.translation import gettext_lazy as _
CHOICES_BOOLEANO_SIM_NAO = (
(True, _('Sim')),
(False, _('Não'))
)
class modelo(models.Model):
"""Model definition for LoteModalidadeEvento."""
# TODO: Define fields here
e_bool_field= models.BooleanField(verbose_name=_('Este é um campo booleano'), **choices**=CHOICES_BOOLEANO_SIM_NAO)
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