2011-03-18 61 views

回答

8
reduce(operator.or_, Q(**{key + '__icontains': val}) for (key, val) in D.iteritems()) 
+1

不錯,簡潔。 – JHSaunders 2011-03-18 10:52:33

+0

雙星號是什麼意思? – 2011-07-29 14:53:59

+0

@ Thr4wn:[Keyword expansion。](http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists) – 2011-07-29 20:07:54

0

有一個更加務實的態度,一個QuerySet ,因爲我需要從一個生成各種密鑰。

query = None 
for key, value in d.iteritems(): 
    if query is None: 
     query = Q(**{key + "__icontains" : value}) 
    else: 
     query |= Q(**{key + "__icontains" : value}) 
相關問題