开发者

Best way to optimize queries like this in Django

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 t开发者_如何学Cwo 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?


tedList = [person for person in namelist if person.name == 'ted']

This will filter the initial QueryList on the client side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜