2009-11-22 110 views

回答

0
>>> u=User.objects.get(pk=1) 
>>> u.is_active 
1 
>>> u.is_active==1 
True 
>>> 

布尔列返回1或0的原因在您的问题的链接。

+0

你的例子应该是:u.is_active == True – 2009-11-22 15:11:51

+0

有没有可能将布尔值字段隐藏为True或False而不是1或0 – 2009-11-23 05:14:03

+0

Juanjo,我列举了一个例子来说明如何实现True或False结果。 拉玛,我想应该可以通过修改Django的模型代码,但我不知道这样的解决方案。 – fest 2009-11-23 18:41:39

5

你可以为你的模型,评估此为你创建你自己的方法:

class User(models.Model): 
    active_status = models.BooleanField(default=1) 

    def is_active(self): 
     return bool(self.active_status) 

那么你对这个领域进行任何测试可能只是参考,而不是方法:

>>> u.is_active() 
True 

你可以甚至把它变成一个属性:

class User(models.Model): 
    active_status = models.BooleanField(default=1) 

    @property  
    def is_active(self): 
     return bool(self.active_status) 

因此,类的用户d on't甚至要知道,它是作为一种方法来实现:

>>> u.is_active 
True 
1

这里是适合NullBooleanField上述方法:

result = models.NullBooleanField() 

def get_result(self): 
    if self.result is None: 
     return None 
    return bool(self.result) 
1

有没有什么预期,这将导致不同的行为只是一种局面基于类型?

>>> 1 == True 
True 
>>> 0 == False 
True 
>>> int(True) 
1 
>>> int(False) 
0