Christopher Nelson Christopher Nelson - 1 year ago 305
Python Question

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

I want to query services between two dates and sum their prices. When I try to use

, 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))

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download