假设我有这个字典
mydict['number'] = {23,24,25}
mydict['name'] = {"john","mike","kaff"}
mydict['area'] = {"london", "usa", "japan"}
我已经有查询集qs
但是我想在上面的dictionary
基础上进行额外的过滤,比如
qs.filter (where number __icontains in 23,24,25)
和qs.filter (where name __icontains in john, mike, kaff)
等
这是我尝试过的,但很粗糙
query_list = []
for key1 in mydict.iteritems():
for myval in mydict[key1]:
qry = qry + Q(myval__icontains= myval) |
query_list.append(qry)
queryset = Student.objects.filter(reduce(operator.and_, query_list))
我将把Q
对象的参数组合到filter
。https://docs.djangoproject.com/en/dev/topics/db/queries/complex-lookups-with-q-objects
您可以尝试以下方法:
for k, v in mydict:
qs.filter('%s__icontains' % k=v)