Django BooleanField value
I have a form field like so
IS_EMPLOYED_CHOICES = (
('1', 'yes'), ('0', 'no')
)
class AddEmployee(forms.Form):
is_employed = forms.ChoiceField(choices=IS_EMPLOYED_CHOICES)
and the field in the model is a BooleanField
is_employed = models.BooleanField()
I am aware that a开发者_高级运维 BooleanField
is a varchar with either a '1'
or '0'
for True
or False
However I want to pass this value from the model to the form field above so it would show no
when it is False and yes
when it is true.
emp_profile = Employees.objects.get(pk=1)
emp_form = AddEmployee(initial={
'is_employed' = emp_profile.is_employed
})
does not work
When the value actually comes out, it's True
and False
instead of '1'
and '0'
.
So you would do something like
IS_EMPLOYED_CHOICES = (
(True, 'yes'), (False, 'no')
)
and it should work
I think it will work if you remove the quotes : IS_EMPLOYED_CHOICES = ( (1, 'yes'), (0, 'no') ) (nod) Good luck
精彩评论