At Django, a boolean field in MySQL is stored as a TINYINT. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour?
A:
>>> u=User.objects.get(pk=1)
>>> u.is_active
1
>>> u.is_active==1
True
>>>
The reasons why boolean columns return 1 or 0 are on the link in your question.
fest
2009-11-22 15:07:54
Your example should have been: u.is_active == True
Juanjo Conti
2009-11-22 15:11:51
Is it possible to implicity covert the boolean fields to True or False instead of 1 or 0
Rama Vadakattu
2009-11-23 05:14:03
Juanjo, I listed that as an example how to achieve True or False result.Rama, I guess it should be possible by modifying Django's model code, but I'm not aware of such solution.
fest
2009-11-23 18:41:39
A:
You could create your own method for your model that evaluates this for you:
class User(models.Model):
active_status = models.BooleanField(default=1)
def is_active(self):
if self.active_status:
return True
else:
return False
Then any tests you perform against this field could just reference the method instead:
>>> u.is_active()
True
So instead of calling an attribute, you're calling a method (extra ()
!!), but this gives you the result you desire.
jathanism
2009-11-25 18:19:43