Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up Django admin page?

I have a model with a table that has around 5 million records. Currently django admin is very slow for the list display page. It can take a minute or more for the page to load.

Is there a way to optimize the list display page? And make it load under 10 seconds? Thanks in advance.

models.py

class Notification(models.Model):
    id = models.AutoField(primary_key=True)
    token = models.ForeignKey(Token)
    alert = models.ForeignKey(Alert)
    created_at = models.DateTimeField(auto_now=True)
    is_sent = models.BooleanField(default=False)
    is_processed = models.BooleanField(default=False)
    error_sending = models.BooleanField(default=False)

    # ...
    def __unicode__(self):
        return u'%s' % (self.id )

admin.py

class AppNotification(admin.ModelAdmin):
    fields = ['is_sent','is_processed','error_sending']

    #
    list_display = ('token_code','is_sent','is_processed')

    #
    search_fields = ('token','alert')

    #
    list_select_related = ('alert', 'token')

    #
    list_per_page = 30

admin.site.register(Notification,AppNotification)

Django Version 1.6

Solution

class AppNotification(admin.ModelAdmin):
    readonly_fields = ('token','alert')

    fields = ['is_sent','is_processed','error_sending','token','alert']

    #
    list_display = ('id','is_sent','is_processed','error_sending')

    #
    search_fields = ['token__token']

    #
    list_per_page = 50

admin.site.register(Notification,AppNotification)
like image 304
ipegasus Avatar asked Jan 30 '26 08:01

ipegasus


2 Answers

The issue is probably the join on the token foreign key. You may want to remove that token_code from the list_display, if possible and completely remove your list_select_related property,

At the very least, remove the 'alert' from list_select_related property, since you don't even use it in the list view.

like image 79
Matt Williamson Avatar answered Feb 01 '26 20:02

Matt Williamson


Remove:

list_select_related = ('alert', 'token')

list_select_related will call select_related. But looking up all related alerts an tokens is redundant.

Why did you truncate the Notification model?

# ... <-- Maybe there is inefficient logic over here!
like image 28
allcaps Avatar answered Feb 01 '26 20:02

allcaps