[Answered ]-Django query to retrieve an initial queryset with subsequent queries on that queryset

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.

πŸ‘€miki725

Leave a comment