[Django]-Django: Most Efficient Way To Filter A Queryset Repeatedly

3👍

You can check prefetch_related it might help you:

This has a similar purpose to select_related, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different… prefetch_related, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related…

So in the end you will either do multiple queries or use prefetch_related and it will do some Python joins on the objects.

👤mpcabd

2👍

You might do something like this:

# This should require two database queries, one for the items 
# and one for all the associated tags.
items = Item.objects.filter(type='type_a').prefetch_related('tags')

# Now massage the data into your desired data structure.
from collections import defaultdict
tag_dict = defaultdict(list)
for item in items:
    # Thanks to prefetch_related this will not hit the database.
    for tag in item.tags.all():
        tag_dict[tag].append(item)

Leave a comment