更新时间:2022-11-10 13:47:09
您可以为您的模型创建自己的方法,为您评估: / p>
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):
return bool(self.active_status)
然后,您对此字段执行的任何测试都可以引用该方法:
Then any tests you perform against this field could just reference the method instead:
>>> u.is_active()
True
您甚至可以将其设为属性: p>
You can even make this into a property:
class User(models.Model):
active_status = models.BooleanField(default=1)
@property
def is_active(self):
return bool(self.active_status)
,以便该类的用户甚至不必知道它作为一种方法实现:
so that users of the class don't even have to know that it's implemented as a method:
>>> u.is_active
True