我如何在Django中做基于字典和它的键的Q过滤器搜索



假设我有这个字典

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)

相关内容

  • 没有找到相关文章

最新更新