繁体   English   中英

hybrid_property在if / else中引发TypeError

[英]hybrid_property raises a TypeError in if/else

我想对混合属性中的id进行一些格式化,以便10以下的数字以P0为前缀(P01,P02、03等)。 以下代码TypeError: Boolean value of this clause is not defined__bool__中的__bool__方法TypeError: Boolean value of this clause is not defined 我想念什么?

在我的models.py中:

@hybrid_property
def conversion_number(self):
    return 'P0{}'.format(self.id) if self.id < 10 else 'P{}'.format(self.id)

这是我的解决办法:

@property
def conversion_number(self):
    return 'P0{}'.format(self.id) if self.id < 10 else 'P{}'.format(self.id)

问题似乎是当sqlalchemy初始化模型时,id是InstrumentedAttribute(不是int)。 这种行为很奇怪,因为我的实现与SQLAlchemy文档中的示例没有太大不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM