1

For this code:

people_pool = People.objects.filter(last_name__in = last_names)
for first_name in first_names:
    for person in people_pool.filter(first_name = first_name):
       # do something with each person. 

My understanding from django querysets is that a query won't actually be executed until you "need" the data, thus allowing for optimization of chained querysets. However, if I'm not mistaken, that would seems to work against me here; the first query that will be executed is essentially going to be the equivalent of:

People.objects.filter(last_name__in = last_names, first_name = first_name) # for the first first_name in first_names

And the database will need to be queried for every single first name. If this is the case, what is the proper way to get Django to actually retrieve the people_pool and then run subsequent filters on the retrieved python objects, leaving the DB alone?

2
  • what are you trying to get to? You may want to look into select_related Commented Oct 14, 2012 at 1:07
  • I'm not trying to get any properties of related models. Just trying to access person's direct fields, but hoping to somehow limit the number of queries that get run by having the filtering executed by python rather than as a separate query. Commented Oct 14, 2012 at 4:41

1 Answer 1

2

In order not to do a db query for each first name, the only way is to do the actual filtering in Python:

people_pool = People.objects.filter(last_name__in = last_names)
people = {}

for person in people_pool:
    first_name = person.first_name
    if first_name in first_names:
        if not first_name in people:
            people[first_name] = []
        people[first_name].append(person)

for first_name, people_first_name in people.items():
    for person in people_first_name:
        # do something with each person

If you want, you can also do something in the first loop (for person in people_pool). I just add people in a dict but that step is not necessary.

Sign up to request clarification or add additional context in comments.

3 Comments

For some reason I was under the impression that if you already had a cached queryset, and then further filtered that queryset, that it wouldn't have to run another query on the database. Am I mistaken?
Yes. The queryset caches the actual query parameters so later you can modify them. It does not cache the actual query (well, it does but only after the first time you get object from queryset). docs.djangoproject.com/en/dev/topics/db/queries/…
Indeed, I was mistaken. Thanks for setting me on the right path. See also, seeknuance.com/2010/12/10/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.