python - What is the best way of creating a list of querysets in django? -



python - What is the best way of creating a list of querysets in django? -

i have model class "place", has field "state", , list of querysets grouped available distinct states in db.

i doing this:

place_list = [] places = place.objects.distinct('state') [place_list.append( place.objects.filter(state=p.state) ) p in places]

is there improve aggregate command utilize optimizing this? best way this?

~ using python 2.6, django 1.3.1

from collections import defaultdict places_by_state = defaultdict(list) place in place.objects.all(): places_by_state[place.state].append(place) list_of_places_by_state = places_by_state.values()

that hits database once, rather 1 time per state original version (assuming utilize results), end list of lists instead of list of querysets.

python django

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -