QuerySets in Dictionaries

Simon Brunning simon at brunningonline.net
Thu Nov 12 14:06:33 EST 2009


2009/11/12 scoopseven <mark.kecko at gmail.com>:
> I need to create a dictionary of querysets.  I have code that looks
> like:
>
> query1 = Myobject.objects.filter(status=1)
> query2 = Myobject.objects.filter(status=2)
> query3 = Myobject.objects.filter(status=3)
>
> d={}
> d['a'] = query1
> d['b'] = query2
> d['c'] = query3
>
> Is there a way to do this that I'm missing?

Untested:

wanted = (('a', 1), ('b', 2), ('c', 2))
d = dict((key, Myobject.objects.filter(status=number)) for (key,
number) in wanted)


-- 
Cheers,
Simon B.



More information about the Python-list mailing list