【发布时间】:2017-02-27 12:07:01
【问题描述】:
我正在尝试使用这样的 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
我应该怎么做?
【问题讨论】:
标签: python sqlalchemy