假设我有这本词典
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))
答案 0 :(得分:0)
我会将参数与filter
个Q
对象组合起来。 https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
答案 1 :(得分:0)
您可以尝试这种方法:
for k, v in mydict:
qs.filter('%s__icontains' % k=v)