如何加快Django管理页面?

时间:2013-12-12 16:21:40

标签: python django django-admin

我有一个包含大约500万条记录的表的模型。目前django admin对于列表显示页面来说非常慢。页面加载可能需要一分钟或更长时间。

有没有办法优化列表显示页面?并在10秒内加载?提前谢谢。

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

解决方案

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)

3 个答案:

答案 0 :(得分:2)

卸下:

list_select_related = ('alert', 'token')

list_select_related将调用select_related。但查找所有相关警报,令牌是多余的。

为什么要截断通知模型?

# ... <-- Maybe there is inefficient logic over here!

答案 1 :(得分:2)

问题可能是加入token外键。如果可能,您可能希望从token_code删除list_display,并完全删除您的list_select_related媒体资源,

至少,请从'alert'属性中删除list_select_related,因为您甚至不在列表视图中使用它。

答案 2 :(得分:1)

添加show_full_result_count = False

Documentation