Reputation: 4047
I am trying to use this code as OR operator with flask and sql alchemy.
.filter(
(Model.c != True, Model.d == None )
|
(Model.c == True, Model.d != None )
)
TypeError: unsupported operand type(s) for |: 'tuple' and 'tuple'
However it seems this syntax only works with one parameter. Something like works fine:
.filter((Model.a != X) | (Model.a == Y))
So my question is how can I add a filter condition to select the combination of the first sequence or the second.
Upvotes: 0
Views: 95
Reputation: 885
With only commas, you're using the |
operator on two tuples, wich is unsupported.
You need to use SQLAlchemy and_
function:
from sqlalchemy import and_
filter(and_(x == 'a', y == 'b'))
Quick edit: you can chain it with, for exemple, or_
to achieve something like this:
from sqlalchemy import or_, and_
filter(
or_(
and_(x == 'a', y == 'b'),
and_(x == 'c', y == 'd')
)
)
Upvotes: 1
Reputation: 65
Your mistake lies with the comma you used. When you used the comma, python interprets your code as a tuple of booleans. You can't pass such tuples through or. you should connect the two conditions with a boolean operator like and
or
and not
, to form a boolean answer that can be passed through the or
operator
Upvotes: 0