首页 > 解决方案 > 模型标签内的Django ORM - 速度

问题描述

使用 Django 1.11 和 python 2.7,我有一个沉重的 Django 管理应用程序,代码如下:

    # same as __str__ in python 3
    def __unicode__(self):
    redirected = "REDIRECTED-" if self.is_redirected else ""
    return u'[{}{}] {}'.format(redirected, self.typecode, self.headline)


    @property
    def is_redirected(self):
        return OtherModel.objects.filter(old_path=self.url).exists()

问题是因为模型标签(打印对象时默认作为输出的名称)在管理模板中的很多地方都使用了,这每次调用 ORM 每次消耗约 200 毫秒,总共 8 次到 ~每页 2 秒,我想加快速度并减少通话。

显而易见的答案是缓存,但调用我们的缓存也需要时间,因为它在不同的服务器上,因此我将获得 8 个外部请求,而不是获得 8 个 SQL 调用,这是我想要避免的另一件事。

我不会详细说明,但我需要将所有内容都留在unicode函数中。

标签: djangodjango-models

解决方案


您可能会使用Exists注释。

from django.db.models import OuterRef, Exists

class NewModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        queryset = super(NewModelAdmin, self).get_queryset(request)
        old_model = OldModel.objects.filter(old_path=OuterRef('url'))
        return queryset.annotate(
            has_old_model=Exists(old_model),
        )


class NewModel(models.Model):
    ...
    @property
    def is_redirected(self):
        if hasattr(self, 'has_old_model'):
            return self.has_old_model
        # Fall back to previous method if you're worried
        # you don't have all paths covered.
        return OtherModel.objects.filter(old_path=self.url).exists()

推荐阅读