我正在尝试从递归函数的输出中获取单个查询集,但遇到了性能问题。
基本上,尝试组合单个查询集的行为似乎使处理时间增加了一倍(由于实施,我期待这一点)但我想知道我是否可以更有效地做到这一点。
def intersect(self, list_of_querysets):
combined = list_of_querysets[0]
for queryset in list_of_querysets:
combined = combined | queryset
return [combined]
def _get_template_folders(self, template_folder_list):
"""
:rtype : list
"""
parents = []
for template_folder in template_folder_list:
if not TemplateFolder.objects.filter(pk=template_folder).exists():
continue
templates = TemplateFolder.objects.filter(pk=template_folder)
for template in templates:
parent_folders = self._get_template_folders([template.template_folder_parent_id])
if parent_folders is not None:
parents.extend(parent_folders)
if templates is not None:
parents.append(templates)
if parents:
return parents
else:
return None
template_folders_list = self.intersect(self._get_template_folders(template_folder_list))