django - 模型标签内的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函数中。
解决方案
您可能会使用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()
推荐阅读
- typescript - 简单的倒数计时器打字稿
- apache-cayenne - 如何在另一个 DataMap 中创建与 ObjEntity 的关系?
- react-native - AsyncStorage 返回 [object object] - 反应原生
- javascript - Append 只是打印文本而不是从我的 api 打印数据...请仅使用 vanilla JS 解决
- python - RuntimeWarning: 'str' 和 'int' 的实例之间不支持'<' - 当减去两个浮点数据帧时
- java - 两种加载 FXML 的方式;为什么一个比另一个更受欢迎?
- r - devtools::check() 产生警告:在输入连接上发现无效输入
- java - 如何导入 Talend ETL 项目
- r - R:用特定文本解析嵌套括号
- javascript - Vue选择槽范围未定义