[英]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.