Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQLAlchemy Multiple Where Condition

I have an Orders table. And I want to write a query to this table with more than one condition. For example, I want to get an order with id field 1 and order_status field 3.

Is there a way to do this with SQL Alchemy?

P.S. I am able to check only one condition with the following query.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

order = Orders.query.filter_by(restaurant_id = restaurant_id).all()

>Solution :

There are multiple ways of doing this for example if you want to apply an and operation you can simply use filter chain as below

order = Orders.query.filter(restaurant_id = restaurant_id).filter(id = 1).filter(order_status = "Status").all()

You can also use and_ and or_ to apply respective conditions like below.

cond = and_(restaurant_id = restaurant_id, id = 1)
order = Orders.query.filter(or_(cond, order_status = "Status")).all()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading