简体   繁体   English

具有多种组合的 OR 运算符

[英]OR operator wth multiple combinations

I am trying to use this code as OR operator with flask and sql alchemy.我正在尝试将此代码用作 flask 和 sql 炼金术的 OR 运算符。

.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.所以我的问题是如何将过滤条件添加到 select 第一个序列或第二个序列的组合。

With only commas, you're using the |只有逗号,您正在使用| operator on two tuples, wich is unsupported.两个元组上的运算符,这是不受支持的。

You need to use SQLAlchemy and_ function:您需要使用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:快速编辑:例如,您可以将其链接到or_以实现如下目的:

from sqlalchemy import or_, and_
filter(
    or_(
        and_(x == 'a', y == 'b'),
        and_(x == 'c', y == 'd')
    )
)

Your mistake lies with the comma you used.您的错误在于您使用的逗号。 When you used the comma, python interprets your code as a tuple of booleans.当您使用逗号时,python 将您的代码解释为布尔元组。 You can't pass such tuples through or.你不能通过 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您应该将这两个条件与 boolean 运算符(例如and ornot )连接起来,以形成可以通过or运算符传递的 boolean 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM