Django is creating duplicate queries for prefetch_related

233 visualizzazioni
Passa al primo messaggio da leggere

Dheerendra Rathor

da leggere,
12 feb 2016, 22:18:2212/02/16
a Django users
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? 

Tim Graham

da leggere,
13 feb 2016, 21:49:4413/02/16
a Django users
Maybe https://code.djangoproject.com/ticket/25546 fixes it? This will be in Django 1.10.

Dheerendra Rathor

da leggere,
13 feb 2016, 22:19:3313/02/16
a Django users
Thanks Tim, from the ticket and Patch it looks like this was my issue. 

--
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...@googlegroups.com.
To post to this group, send email to django...@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/a8a7e2f6-999f-4eaa-a045-bcf187c11b94%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Rispondi a tutti
Rispondi all'autore
Inoltra
0 nuovi messaggi