Tuning your Django admin site

The good thing about Django is that the admin page is powerful, but if you use it properly, ORM will issue miscellaneous SQL, so take care. I'm just starting to use it, so I'll add it as needed.

Optimize SQL for list page by overwriting queryset

Select_related is enabled if list_display contains a ForeignKey field. However, select_related does not follow the relation of FK field with null = True, so the target is fetched by individual SQL for the number of records. Therefore, when the list page is opened, the SELECT statement is issued about 400 times. Also, select_related traces the FK field with null = False indefinitely, so it may be SQL that JOINs with 10 or 20 tables.

To do it well, define queryset yourself. With this, you can join as much as necessary.

admin.py


class UserRelationshipAdmin(admin.ModelAdmin):
    list_display = ('id', 'from_user', 'to_user', 'is_block', 'is_friend', 'created_at')
    
    def queryset(self, request):
        return super(UserRelationshipAdmin,
                 self).queryset(request).select_related('from_user', 'to_user')

Specify raw_id_fields for items with a large number of cases

Foreign Key and choices fields are displayed by pull-down (select element) on the detail page by default. It will be great if tens of thousands of tables are referenced, so if you specify raw_id_fields, it will display a text box + label.

admin.py


class UserProfileAdmin(admin.ModelAdmin):
    raw_id_fields = ('user',)

There is a description here in the official document https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields

Recommended Posts

Tuning your Django admin site
[Django] Want to customize your admin page?
Initialize your Django app
Create your own Django middleware
Install Django on your Mac
Django admin screen list_filter customization
When you forget your admin screen username / password in Django
Django admin screen reverse lookup memo
Analyze your site using Google Analytics-Chapter 1
Register your Django application in your project
Deploy your Django application on Heroku
Django Tips-Create a ranking site with Django-
[Django] Create your own 403, 404, 500 error pages
Django admin screen customization first step