refactor: 优化查询

pull/102/head
Angelo 2023-07-08 16:03:47 +08:00
parent 0c301ef3a7
commit 554f3e1dc8
1 changed files with 22 additions and 37 deletions

View File

@ -372,37 +372,33 @@ def calculate_execution_time(func):
return wrapper
def get_children(model: models, obj_id: int, all_qs=None, rec_list=None):
if not all_qs:
all_qs = model.objects.all().values("id", "parent")
if rec_list is None:
rec_list = [obj_id]
for ele in all_qs:
if ele.get("parent") == obj_id:
rec_list.append(ele.get("id"))
get_dept(ele.get("id"), all_qs, rec_list)
return list(set(rec_list))
# def get_children(model: models, obj_id: int, all_qs=None, rec_list=None):
# if not all_qs:
# all_qs = model.objects.all().values("id", "parent")
# if rec_list is None:
# rec_list = [obj_id]
# for ele in all_qs:
# if ele.get("parent") == obj_id:
# rec_list.append(ele.get("id"))
# get_dept(ele.get("id"), all_qs, rec_list)
# return list(set(rec_list))
@calculate_execution_time
def get_qs_children(model, qs):
dept_ids = []
for d in qs:
r = get_children(model, d.id)
dept_ids.extend(r)
return list(set(dept_ids))
# @calculate_execution_time
# def get_qs_children(model, qs):
# dept_ids = []
# for d in qs:
# r = get_children(model, d.id)
# dept_ids.extend(r)
# return list(set(dept_ids))
@calculate_execution_time
def next_layer_data(qs_filter, qs_node):
print(f"过滤查询集 ==> {qs_filter}", flush=True)
print(f"当前传入的待渲染节点 ==> {qs_node}", flush=True)
parent_nodes = set(qs_node.values_list("id", flat=True))
print(f"待渲染节点的id ==> {parent_nodes=}", flush=True)
# print(f"过滤查询集 ==> {qs_filter}", flush=True)
# print(f"待渲染节点的id ==> {parent_nodes=}", flush=True)
if set(qs_filter) == set(qs_node):
return parent_nodes
# qs_filter内所有父级id 去重
parent_ids = set()
for node in qs_filter:
@ -414,9 +410,7 @@ def next_layer_data(qs_filter, qs_node):
parent_ids.add(node.parent.id)
break
node = node.parent
# parent_ids.add(node.id)
print(f"过滤查询集的父节点id ==> {parent_ids=}", flush=True)
# print(f"过滤查询集的父节点id ==> {parent_ids=}", flush=True)
return parent_ids
@ -455,6 +449,7 @@ class LazyLoadFilterSetMetaclass(FilterSetMetaclass):
class LazyLoadFilter(FilterSet, metaclass=LazyLoadFilterSetMetaclass):
# @calculate_execution_time
@property
def qs(self):
queryset = self.queryset
@ -464,17 +459,7 @@ class LazyLoadFilter(FilterSet, metaclass=LazyLoadFilterSetMetaclass):
self.form.cleaned_data.pop("parent", None)
print(queryset, flush=True)
if self.form.cleaned_data:
all_dept = queryset.count() == queryset.model.objects.all().count()
p_set = set(queryset.values_list("parent", flat=True))
all_root = len(p_set) == 1 and p_set.pop() is None
is_root = all_dept or all_root
ids = (
list(set(queryset.model.objects.all().values_list("id", flat=True)))
if is_root
else get_qs_children(queryset.model, queryset)
)
# print(f"{ids=}", flush=True)
self.queryset = queryset.model.objects.filter(id__in=ids)
self.queryset = queryset.model.objects.all()
node_ids = next_layer_data(super().qs, queryset)
return queryset.model.objects.filter(id__in=node_ids)
return super().qs