python - hybrid_property 在 if/else 中引发 TypeError
问题描述
我想对混合属性中的 id 进行一些格式化,以便 10 以下的数字以 P0 为前缀(P01、P02、03 等)。以下代码TypeError: Boolean value of this clause is not defined
从__bool__
elements.py 中的方法抛出一个。我错过了什么?
在我的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 文档中的示例没有本质区别。
推荐阅读
- apache-spark - 使用 Spark 如何在末尾添加列
- php - 显示假结果,而查询返回真
- r - 有一条线穿过它们的子图
- bluetooth-lowenergy - BLE 读取不正确?
- swift - CoreML 模型预测不同于训练
- java - JDK8 -> JDK10:PKIX 路径构建失败:SunCertPathBuilderException:无法找到请求目标的有效认证路径
- laravel - Vuetify - 在点击事件上添加加载层叠加
- apache - 无法访问抛出此站点的 Apache2 虚拟主机
- c# - 在存储过程中添加一对多记录
- ssh - 如何使用 scp 通过 ssh 转发的堡垒从服务器获取文件?