Assume these two models.
class Folder(model):
name = models.CharField(max_length=8)
files = models.ManyToManyField('File')
class File:
path = models.CharField(max_length=255)
First I was fetching the folders and while looping over them fetching the files with a distinct path
folders = Folder.objects.all()
for folder in folders:
files_with_distinct_path = folder.files.distinct('path').order_by('path')
but this suffers from the N+1 problem, So I tried prefetching the files while fetching all the folders.
from django.db.models import Prefetch
folders = Folder.objects.prefetch_related(
Prefetch(
'files', queryset=File.objects.distinct('path').order_by('path'), to_attr='distinct_files'
)
)
but it turns out now there objects with empty `distinct_files` which is not the correct result, these objects have files but after prefetching, the result is empty only for few objects.