zentenk
zentenk

Reputation: 2815

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

Upvotes: 1

Views: 4815

Answers (2)

MightyPixel
MightyPixel

Reputation: 111

I think it will work if you remove the quotes : IS_EMPLOYED_CHOICES = ( (1, 'yes'), (0, 'no') ) (nod) Good luck

Upvotes: 0

bash-
bash-

Reputation: 6304

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

Upvotes: 3

Related Questions