Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

I have a fairly simple Django set up for a forum, and one of the most basic models is this, for each thread:

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. When I run this SQL query:

 SELECT * FROM `forum_post` LIMIT 10000

I see that Query took 0.0241 sec which is fine. 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.

However, if I try and pull up an individual PostReply, it takes around 2-3 minutes to load.

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? 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?

like image 362
Matthew Johnston Avatar asked Jun 09 '14 08:06

Matthew Johnston


People also ask

Is Django admin good for production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.

What is model admin in Django?

It is used to create the form presented on both the add/change pages. You can easily provide your own ModelForm to override any default form behavior on the add/change pages. Alternatively, you can customize the default form rather than specifying an entirely new one by using the ModelAdmin. get_form() method.

Is Django admin necessary?

Show activity on this post. No. The django admin is not intended for any end-user. The django admin feature is intended to assist the website developer, and that is all.


1 Answers

Try to add all foreign keys in raw_id_fields in admin

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.

like image 74
Andrey Nelubin Avatar answered Nov 16 '22 16:11

Andrey Nelubin