Peewee:简单的条件始终为True

时间:2016-08-04 02:25:28

标签: python peewee

我想做这样的事情:

conditions = Model.attribute == Model.attribute
conditions &= (more conditions here)

Model.select().where(conditions)

是否有类似Django's Q object的内容替换第一行?

2 个答案:

答案 0 :(得分:0)

您可以使用以下表达式获得这种条件:

from peewee import Value
...

def TRUE_condition():
   return (Value(1) == Value(1))

...
conditions = TRUE_condition()
conditions &= (Model.attribute == 42)
Model.select().where(conditions)

但是,这看起来像是骇客。更为优雅的解决方案是将所有条件收集在一个列表中,然后使用reduce运算符and对其进行>:

from functools import reduce
from peewee import operator

conditions = list()
conditions.append(Model.attribute == 42)
conditions.append(Model.active == True)
condition = reduce(operator.and_, conditions)
Model.select().where(condition)

答案 1 :(得分:0)

True似乎正在工作:

conditions = True
conditions &= (Model.attribute == 42)
Model.select().where(conditions)