> Den 16/09/2015 kl. 19.08 skrev Simon Charette <charett...@gmail.com>: > > If you want to use this approach I'm afraid you'll have to do > what prefetch_related does under the hood by yourself. > > e.g. > > from collections import defaultdict > > prefetched = defaultdict(list) > subjects = Subject.objects.filter(...) > lessons = Lesson.objects.filter(...) > for lesson_subject in > Lesson.subjects.through.objects.filter(lesson__in=lessons, > subject__in=subject).select_related('subject').iterator(): > prefetched[lesson_subject.lesson_id].append(lesson_subject.subject)
I hacked up a quick solution that works for my limited requirements. No chained querysets, limited prefetch recursion, no order_by, extras etc. But maybe someone finds it useful: https://gist.github.com/ecederstrand/6748a3496acdc95e40ef At least it speeds up my queries exponentially; 10-50x improvement in my tests so far. Erik -- 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 post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6E808964-026D-40B4-BD59-52E13EC4CFF2%40cederstrand.dk. For more options, visit https://groups.google.com/d/optout.