How to prefetch related QuerySet in Django admin

Use prefetch_related and select_related in Django admin. Implement a custom get_queryset method.

from django.contrib import admin
 
class MyModelAdmin(admin.ModelAdmin):
  def get_queryset(self, request):
    queryset = super(MyModelAdmin, self).get_queryset(request)
    queryset = queryset.prefetch_related('user')
    return queryset
This post is available on GitHub.
Posted .

1 comment

Leave a Reply