Querying with function on Flask-SQLAlchemy model gives BaseQuery object is not callable error

微笑、不失礼 提交于 2019-11-26 09:56:44

问题


I want to query services between two dates and sum their prices. When I try to use func.sum with Services.query, I get TypeError: BaseQuery object is not callable. How do I query using a function with Flask-SQLAlchemy?

Services.query(func.sum(Services.price)).filter(Services.dateAdd.between(start, end))

回答1:


Model.query is a shortcut to db.session.query(Model), it's not callable. If you're not querying a model, continue to use db.session.query(...) as you would with regular SQLAlchemy.

db.session.query(db.func.sum(Services.price)).filter(
    Services.dateAdd.between(start, end)
)


来源:https://stackoverflow.com/questions/40918479/querying-with-function-on-flask-sqlalchemy-model-gives-basequery-object-is-not-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!