在姜戈,MySQL 中的布尔字段存储为 TINYINT http://docs.djangoproject.com/en/dev/ref/models/fields/。当我检索它时,我得到 0 或 1。我不应该得到 False 或 True 吗?有没有办法实现这种行为?
您可以为您的模型创建自己的方法来为您评估这一点:
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)
这样该类的用户甚至不必知道它是作为方法实现的:
>>> u.is_active
True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)