0

因为我需要避免递归导入并将Group对象作为查询的起点(这就是我不能Action直接导入对象的原因。)

关系是Group-> Component-> ComponentVersion->Action

例如,Group.components.all()返回查询集中的所有组件。

另外[component.versions.all() for component in Group.components.all()]返回一个查询集列表,其结果是所有版本。随后,.actions.all()将返回理解中返回actions的每个查询集的所有查询集ComponentVersion

避免对数据库进行不必要的调用和提高可读性的最佳方法是什么?

4

1 回答 1

1

预取相关从这里开始,这将运行 4 个查询,但会立即检索所有数据,而不是迭代和进行多个查询。

Group.objects.all().prefetch_related('components', 'components__versions', 'components__versions__actions')
于 2018-06-21T21:38:12.777 回答