feat: 获取子集的性能优化2

This commit is contained in:
caoqianming 2024-06-05 14:07:50 +08:00
parent a54030e9c0
commit 16ead75d3b
2 changed files with 7 additions and 9 deletions

View File

@ -8,10 +8,17 @@ def get_child_queryset_u(checkQueryset, hasParent=True):
查的范围checkQueryset
父obj
是否包含父默认True
若有parent_link字段则进行性能优化
'''
cls = type(checkQueryset.model)
if hasattr(cls, 'parent'):
queryset = cls.objects.none()
if hasattr(cls, 'parent_link'):
for item in checkQueryset:
queryset = queryset | cls.objects.filter(parent_link__contains=item.id)
if hasParent:
queryset = queryset | cls.objects.filter(pk=item.id)
return queryset
if hasParent:
queryset = checkQueryset
child_queryset = checkQueryset.filter(parent__in=queryset)

View File

@ -113,15 +113,6 @@ class CustomGenericViewSet(MyLoggingMixin, GenericViewSet):
queryset = queryset.select_related(*self.select_related_fields)
if self.prefetch_related_fields:
queryset = queryset.prefetch_related(*self.prefetch_related_fields)
# 查询出子集数据,请谨慎使用
has_children = self.request.query_params.get('has_children', 'no')
if has_children == 'hasparent':
queryset = get_child_queryset_u(queryset, True)
elif has_children == 'noparent':
queryset = get_child_queryset_u(queryset, False)
else:
pass
return queryset
def get_queryset(self):