Friday, February 12, 2016

Django is creating duplicate queries for prefetch_related

Hello,

Recently I was monitoring db queries created by Django and a particular block was behaving unusual.
Here is the code block:

        from django.db import connection, reset_queries
        reset_queries()
        group = get_object_or_404(
            Group.objects.select_related('course').prefetch_related(
                Prefetch(
                    'concepts',
                    queryset=Concept.objects.filter(is_published=True).prefetch_related(
                        'videos', 'pages',
                    ),
                ),
            ).only('course', 'playlist'),
            pk=pk,
        )

        s4 = connection.queries
        k4 = len(s4)
        reset_queries()



Now, the interesting part is that prefetch queries are made twice for videos and pages. Hereis the query dump https://paste.kde.org/psrp4orn6

It is making same queries twice with exactly same parameters. 

Can someone please explain it for me? In my view, it should not happen. I'm missing something here? 

--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/41291526-d1bb-47bd-9da4-0ac00c7d989a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment