简体   繁体   English

Django-添加新数据时需要花费很长时间加载的模型页面

[英]Django - model page taking long to load when adding new data

I have a fairly simple model with a many to many relationship to another model. 我有一个相当简单的模型,该模型与另一个模型有很多关系。 rom django.db import models rom django.db导入模型

class MechanismReference(models.Model):
    reference = models.TextField(help_text=u'References to support the target information like PMID, KEGGID,etc.')
    reference_type = models.CharField(max_length=10, blank=True,
                                      help_text='Type of reference, the reference from KEGG, PIMD, PMC or other')
    mech_reference = models.ManyToManyField('Mechanism', blank=True, null=True, max_length=150)


    def sort_data(self):
        return self.mech_reference.order_by('pk')

    def __unicode__(self):
        return u'%s %s (%s)' % (
        self.reference_type, self.reference, ",".join(mechanism.molecule for mechanism in self.mech_reference.all()))

The Mechanism models is like this: 机制模型如下:

class Mechanism(models.Model):
    target = models.ForeignKey('TargetDictionary', blank=True, null=True,
                               help_text=u'Target associated with this mechanism of action (foreign key to target_dictionary table')
    molecule = models.ForeignKey('MoleculeDictionary', blank=False, null=False,
                                 help_text=u'Molregno for the drug (foreign key to molecule_dictionary table)')
    mechanism_type = models.ForeignKey('MechanismType', blank=True, null=True,
                                       help_text=u'Type of action of the drug on the target e.g., agonist/antagonist etc (foreign key to action_type table)')

    class Meta:
        ordering = ['molecule', ]
    def __unicode__(self):
        #
        if self.mechanism_type:
            return u'%s %s %s' % (self.molecule.molecule, self.target.name, self.mechanism_type.description)
        elif self.mechanism_type is None:
            return u'%s %s %s' % (self.molecule.molecule, self.target.name, self.mechanism_type.moa_qualifier)
        else:
            return u'%s %s %s' % (self.molecule.molecule, self.target.name, str(self.pk))

The mechanism model has got 2041 records. 机理模型有2041条记录。 The reference page loads Okie when it executes select * from mechanism_reference. 该参考页在执行Okie时会从选择机制*中加载Okie。 But whenever I add a new mechanism reference it takes so long sometime I get a server error(500). 但是,每当我添加一个新的机制引用时,它花费的时间太长了,我会收到服务器错误(500)。 Obviously, the page would have all the records form Mechanism table pre-populated to enable adding a many to many relationship for a given reference. 显然,该页面将预先填充了机制表中的所有记录,以便为给定的引用添加多对多关系。 Can anyone point me why is it slow and is there a way to reduce the loading up time? 谁能指出我为什么这么慢,有没有办法减少加载时间?

Again this is much slower in development or production server than my localserver. 同样,这在开发或生产服务器中比本地服务器要慢得多。

I use xadmin and individual models doesn't have designated view. 我使用xadmin,各个模型没有指定的视图。 I have added my view and base_site.html. 我已经添加了我的视图和base_site.html。

View: 视图:

from django.shortcuts import render

from django.http import HttpResponse
from django.template import RequestContext, loader

def index(request):
    template = loader.get_template('idg/index.html')
    context = RequestContext(request, {})
    return HttpResponse(template.render(context))

You can try: 你可以试试:

Mechanism.objects.all().prefetch_related('target', 'molecule')

This will prevent 2 extra queries for each Mechanism in the db. 这将防止对数据库中的每个Mechanism进行2个额外的查询。

See docs on prefetch_related for more info about how this works. 有关更多信息, 请参见 prefetch_related 文档

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

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