[Answered ]-Django, many duplicated queries in my TabularInline

2👍

You are facing typical problem with django QuerySets. You got this queries because when you access related object which is not prefetched beforehand you make additional DB hit.

Read more https://docs.djangoproject.com/en/1.10/ref/models/querysets/#select-related and https://docs.djangoproject.com/en/1.10/ref/models/querysets/#prefetch-related

This should solve your problem

class TopicAdmin(admin.ModelAdmin):
    inlines = (TopicAndMediaInline,)

    def get_queryset(self, request):
        qs = super(TopicAdmin, self).get_queryset(request)
        qs = qs.prefetch_related('medias')
        return qs 

Leave a comment