I am trying to take a list of strings and query based an or combination of all the strings in the list. I want to know if something like this can be done below.
def filters(self,filter_company = ["DPL"]):
print "TEST"
# Company Filter
company_conj = 1 ## Bitwise 1 with AND will not effect other AND bits
for c in filter_company:
company_conj = (company_conj) & (Exception.company == c) ## Create co
qrty_exceptions = session.query(Exception).filter(company_conj) ## Query by conjunction
So basically, I am iterating through each item in the list and trying to make a conjunction by concatinating. The point being is that I don't know how many items will be in the list...but I want to logically bitwise AND them together so they all form the conjunction. I end up getting the following error:
TypeError: unsuported operand type(s) for &: 'int' and BinaryExpression
In reality, I mean't to write this using the bitwise OR symbol... "|" but i figure if you can do it for AND then it should work for OR too.
I think the example below should help you:
from sqlalchemy import or_, and_
company_filters = ["Filter1", "Filter2"] # as many as you like
clauses = [(Exception.company == c) for c in company_filters]
q = session.query(Exception)
q = q.filter(and_(*clauses)) # or `or_(*clauses)`