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
Did this help you? Please let me know with a comment. Thank you
Posted .

1 comment (RSS)

Leave a comment