I suspect your usage of `distinct('path')` is causing issues during prefetching here
as it will prevent different File with the same .path from being prefetched for two different
folder objects.
You likely want to use `distinct('path', 'folder')` here instead where 'folder' might have a different
name based on whether or not you provided a related_name or related_query_name to Folder.files.
Cheers,
Simon
Le lundi 30 décembre 2019 02:20:07 UTC-5, Mohit Solanki a écrit :
Assume these two models.class Folder(model): name = models.CharField(max_length=8) files = models.ManyToManyField('File') class File: path = models.CharField(max_length=25
5 )
First I was fetching the folders and while looping over them fetching the files with a distinct pathfolders = 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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac516a2f-a835-4371-b097-be5793b42c6a%40googlegroups.com.
No comments:
Post a Comment