Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy "or" statement with multiple parameters

I have a query that require to use the "or" | operator :

Mymodel.query.filter((Mymodel.a== 'b') | (Mymodel.b == 'c'))

That works fine. However, I want my conditions to be put in an array of unkown length :

conds = [ Mymodel.a== 'b', Mymodel.b == 'c', Mymodel.c == 'd']
Mymodel.query.filter(???(conds))

Thanks !

like image 738
Blusky Avatar asked May 08 '15 12:05

Blusky


1 Answers

You are looking for or_

conds = [ Mymodel.a== 'b', Mymodel.b == 'c', Mymodel.c == 'd']

If you have the above list of conditions just pass them all to or_

from sqlalchemy import or_
Mymodel.query.filter(or_(*conds))
like image 93
Alfred Rossi Avatar answered Oct 27 '22 02:10

Alfred Rossi