Best way to optimize queries like this in Django
Posted
by chris
on Stack Overflow
See other posts from Stack Overflow
or by chris
Published on 2010-06-02T17:01:58Z
Indexed on
2010/06/02
17:03 UTC
Read the original article
Hit count: 400
I am trying to lower the amount of queries that my django app is using, but I am a little confused on how to do it.
I would like to get a query set with one hit to the database and then filter items from that set. I have tried a couple of things, but I always get queries for each set.
let's say I want to get all names from my DB, but also separate out the people just named Ted. Both the names and the ted set will be used in the template.
This will give me two sets, one with all names and one with Ted.. but also hits the database twice:
namelist = People.objects.all()
tedList = namelist.filter(name='ted')
Is there a way to filter the first set without hitting the data base again?
© Stack Overflow or respective owner