在 Django,a boolean field in MySQL is stored as a TINYINT .当我检索它时,我得到 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
关于python - MySQL Django 模型中的 boolean 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1778948/