4
我尝试使用SQLAlchemy的混合属性这样SQLAlchemy的 - 在查询
class Metric(Base):
__tablename__ = 'metric'
id = Column(Integer, primary_key=True)
value = Column(Float, nullable=False)
@hybrid_property
def dominance(self):
return 1 - abs(0.5 - float(self.value))
现在,我用这个在我的模型是这样
class MetricModel(BaseModel):
def get_dominance(self):
self.query(Metric).filter(Metric.dominance > 0.5).order_by(Metric.dominance.desc()).limit(2).all()
这种采用混合性质是一个烧瓶应用程序,它被称为像这样
model = MetricModel(db)
with db.session():
print(model.get_dominant_traits())
这给了我一个错误 TypeError: float() argument must be a string or a number, not 'InstrumentedAttribute'
从错误它看起来像没有结果集,因此失败。我按照这里的文档http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html 我应该做什么不同?