Menu

Fixing Slow Django Admin Pages on Large Tables
📰

Fixing Slow Django Admin Pages on Large Tables

DEV Community: django·sizan mahmud0·4 days ago
#wJBgkf5n
#devtodjango#devto#dev#admin#enter
Reading 0:00
15s threshold

The Django admin is fast on small tables and painful on big ones. Here's a quick checklist for getting it back under control.

Step 0: measure

Use django-debug-toolbar and check query count, duplicates, and the slowest query. For a suspicious query:

print ( Order . objects . filter ( status = "paid" ) . explain ( analyze = True ) )

Enter fullscreen mode

Exit fullscreen mode

N+1 in list_display

@admin . register ( Order ) class OrderAdmin ( admin . ModelAdmin ) : list_display = ( "id" , "customer" , "status" , "total" ) list_select_related = ( "customer" , )

Enter fullscreen mode

Exit fullscreen mode

For more control:

def get_queryset ( self , request ) : return ( super ( ) . get_queryset ( request ) . select_related ( "customer" ) . only ( "id" , "status" , "total" , "customer__name" ) )

Enter fullscreen mode

Exit fullscreen mode

Careful: if __str__ or a display method reads a field you excluded with only() , you get N+1 again.

Custom columns that query per row

from django . db .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.