简体   繁体   English

hybrid_property在if / else中引发TypeError

[英]hybrid_property raises a TypeError in if/else

I'd like to do some formatting on the id in a hybrid property so that numbers under 10 are prefixed with a P0 (P01, P02, 03, etc). 我想对混合属性中的id进行一些格式化,以便10以下的数字以P0为前缀(P01,P02、03等)。 The following code is throwing a TypeError: Boolean value of this clause is not defined from the __bool__ method in elements.py. 以下代码TypeError: Boolean value of this clause is not defined__bool__中的__bool__方法TypeError: Boolean value of this clause is not defined What am I missing? 我想念什么?

In my models.py: 在我的models.py中:

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

This was my fix: 这是我的解决办法:

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

The issue seemed to be that id is an InstrumentedAttribute (not an int) when sqlalchemy is initializing the model. 问题似乎是当sqlalchemy初始化模型时,id是InstrumentedAttribute(不是int)。 This behavior is strange because my implementation isn't substantially different from the example in the SQLAlchemy docs. 这种行为很奇怪,因为我的实现与SQLAlchemy文档中的示例没有太大不同。

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

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