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
Post a Comment