简体   繁体   English

为什么 Django 模型需要这么长时间才能加载到 admin 中?

[英]Why is a Django model taking so long to load in admin?

I have a fairly simple Django set up for a forum, and one of the most basic models is this, for each thread:我为论坛设置了一个相当简单的 Django,对于每个线程,最基本的模型之一是:

class Post(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    last_reply = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    username = models.ForeignKey(User, related_name="forumuser")

    fixed = models.BooleanField(_("Sticky"), default=False)
    closed = models.BooleanField(default=False)
    markdown_enabled = models.BooleanField(default=False)

    reply_count = models.IntegerField(default=0)
    title = models.CharField(_("Title Post"), max_length=255)
    content = models.TextField(_("Content"), blank=False)
    rating = models.IntegerField(default=0)
    followers = models.IntegerField(default=0)

    ip_address = models.CharField(max_length=255)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return "/post/%s/" % self.id

Then we have some replies:然后我们有一些回复:

class PostReply(models.Model):
    user = models.ForeignKey(User, related_name='replyuser')
    post = models.ForeignKey(Post, related_name='replypost')
    created = models.DateTimeField(auto_now_add=True)
    content = models.TextField()
    ip_address = models.CharField(max_length=255)
    quoted_post = models.ForeignKey('self', related_name='quotedreply', blank=True, null=True)
    rating = models.IntegerField(default=0)
    reply_order = models.IntegerField(default=1)

Now, currently there just over 1600 users, 6000 Posts, and 330,000 PostReply objects in the db for this setup.现在,对于此设置,数据库中目前只有 1600 多个用户、6000 个帖子和 330,000 个 PostReply 对象。 When I run this SQL query:当我运行此 SQL 查询时:

 SELECT * FROM `forum_post` LIMIT 10000

I see that Query took 0.0241 sec which is fine.我看到Query took 0.0241 sec ,这很好。 When I browse to the Django admin section of my site, pulling up an individual Post is rapid, as is the paginated list of Posts.当我浏览到我网站的 Django 管理部分时,拉出一个单独的帖子很快,帖子的分页列表也是如此。

However, if I try and pull up an individual PostReply, it takes around 2-3 minutes to load.但是,如果我尝试拉出单个 PostReply,则加载大约需要 2-3分钟

Obviously each PostReply admin page will have a dropdown list of all the Posts in it, but can anyone tell me why this or anything else would cause such a dramatically slow query?显然,每个 PostReply 管理页面都会有一个包含所有帖子的下拉列表,但是谁能告诉我为什么这个或其他任何东西会导致查询如此缓慢? It's worth noting that the forum itself is pretty fast.值得注意的是,论坛本身的速度相当快。

Also, if it is something to do with that dropdown list, has anyone got any suggestions for making that more usable?另外,如果事做与下拉列表中,有没有人得到了使更多的可用什么建议?

Try to add all foreign keys in raw_id_fields in admin尝试在 admin 中的raw_id_fields中添加所有外键

class PostReplyAdmin(ModelAdmin):
     raw_id_fields = ['user', 'post', 'quoted_post']

This will decrease page's load time in change view.这将减少页面在更改视图中的加载时间。 The problem is that django loads ForeignModel.objects.all() for each foreign key's dropdowns.问题是 django 为每个外键的下拉列表加载 ForeignModel.objects.all() 。

Another way is to add foreign keys in autocomplete_fields ( docs ) in admin另一种方法是在 admin 的autocomplete_fields ( docs ) 中添加外键

class PostReplyAdmin(ModelAdmin):
    autocomplete_fields = ['user', 'post', 'quoted_post'] 

As pointed by @Andrey Nelubin the problem for me was indeed in the page loading all related models for each foreign key's dropdown.正如@Andrey Nelubin 所指出的,我的问题确实出在为每个外键的下拉列表加载所有相关模型的页面中。 However, with autocomplete_fields selects are turned into autocomplete inputs (see figure below), which load options asynchronously.然而,使用autocomplete_fields选择被转换为自动完成输入(见下图),异步加载选项。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM