1

假设我有这本词典

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))
4

2 回答 2

0

您可以尝试这种方法:

for k, v in mydict:
    qs.filter('%s__icontains' % k=v)
于 2013-04-18T06:39:17.360 回答
0

我会将论点与filterfromQ对象结合起来。https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

于 2013-04-18T06:13:24.017 回答