Is there a way to conditionally add filter
arguments to a query in the SQL Alchemy ORM?
For example imagine, I have the following:
q = session.query(X)
if a:
q.filter(X.y == 'a')
elif b:
q.filter(X.y == 'a', X.z == 'b')
elif c:
q.filter(X.y == 'a', X.p == 'd')
Is there a way to say just add
X.z == 'b'
if b
without having to readd (X.y == 'a')
in every filter.
It seems that I could do
q.filter(X.y == 'a').filter(X.y == 'b')
but this changes the query that is being performed.