簡體   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