我想用这样的过滤器查询我的Django模型:
(pseudo code)
field1values = ['val11','val12']
field2values = ['val21','val22']
result = (field1 == 'val11' | field1 == 'val12') & (field2 == 'val21' | field2 == 'val22')
如果我只在这里查询field1,代码可能如下所示:
clauses = []
for item in field1values:
clauses.append(MyModel.objects.filter(field1=item))
result = reduce(lambda x, y: x | y, clauses)
但我无法弄清楚如何为我想要的复杂谓词创建过滤器
答案 0 :(得分:5)
resultquery = (Q(field1='val11') | Q(field1='val12')) & ...
result = SomeModel.objects.filter(resultquery)
另外,in
。