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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKB6Ln8ECMFriry%3DMa3RwJobXNb7Od0wdwM2JvrETaOiOQAOhw%40mail.gmail.com.

Reply via email to