I'm trying to get a single queryset from the output of a recursive function and i am hitting a performance problem.
Basically it seems that the act of trying to combine the individual querysets is doubling the time it takes to process (I was expecting this because of the implementation) but i was wondering if i could do this more efficiently.
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))