【发布时间】:2015-06-01 21:42:00
【问题描述】:
我有一个 SQLAlchemy 模型,如下所示,我正在尝试过滤 isActive 属性。
query(PersonMedications).filter(PersonMedication.isActive==False).all()
class PersonMedication(ModelAbstract):
__tablename__ = "personMedication"
id = db.Column(db.Integer, primary_key=True)
startDate = db.Column(db.Date)
endDate = db.Column(db.Date)
isCanceled = db.Column(db.Boolean)
@hybrid_property
def isActive(self):
if self.isCanceled == True:
return False
elif self.endDate and self.endDate < datetime.date.today():
return False
elif self.startDate and self.startDate <= datetime.date.today():
return True
else:
return False
我收到以下错误:
TypeError: Boolean value of this clause is not defined
从查看 SQLAlchemy 文档看来,我的函数应该可以工作。我错过了什么?
http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/hybrid.html
更新
根据@van 的建议,我发现我需要使用表达式函数,但似乎无法找出将逻辑串在一起的正确语法。
@isActive.expression
def isActive(cls):
return not cls.isCanceled and cls.startDate <= func.current_date()
【问题讨论】:
标签: sqlalchemy