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?
select_related