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

Django-添加新数据时加载模型页面需要很长时间,django,django-models,Django,Django 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=1

我有一个相当简单的模型,与另一个模型有多对多的关系。 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()))
机制模型如下所示:

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))
该机制模型已获得2041条记录。引用页面在执行select*from mechanism\u引用时加载Okie。但每当我添加一个新的机制引用时,我都会遇到一个服务器错误(500)。显然,页面将预先填充所有记录表单机制表,以便为给定引用添加多对多关系。有人能告诉我为什么速度慢,有没有办法缩短加载时间?

同样,在开发或生产服务器中,这比我的localserver慢得多

我使用xadmin,而单个模型没有指定的视图。我已经添加了我的视图和base_site.html

视图:

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))
您可以尝试:

Mechanism.objects.all().prefetch_related('target', 'molecule')
这将防止对数据库中的每个
机制进行2次额外查询


prefetch\u related
上查看有关此操作的详细信息。

您应该显示您的视图。您可以使用django调试工具栏检查生成的SQL查询。1923年的SQL-5396查询。84MS
index
不获取任何机制对象。您可以添加实际执行此操作的视图吗?我已经在
def\uuuu unicode\uuuuuu(self)中得到了这一点:返回u“%s%s(%s)”%(self.reference\u type,self.reference,“,”。join(self.mech\u reference.all()中的mechanism.molector for mechanism)(这在我查看机制引用时会获取)。但是当我尝试添加引用时,我会将它们作为单个元素获取。在unicode方法中不应该执行
self.mech\u reference.all()
。这会在打印对象时导致大量不必要的查询。(或者在模板中执行例如
{{obj}}
)对不起,我仍然不明白如果不是unicode,我应该如何合并预取相关。我是django的新手,如果你能再详细一点的话,我会很有帮助的。谢谢您的时间。您应该在查看功能中添加与预回迁相关的内容。从何处获取所有MechanismReference对象。能否将完整的views.py添加到问题中?