Browse Source

perf: 优化计算数量

pull/6569/head
ibuler 3 years ago committed by 老广
parent
commit
b56b897260
  1. 25
      apps/applications/models/application.py

25
apps/applications/models/application.py

@ -1,3 +1,5 @@
from collections import defaultdict
from django.db import models from django.db import models
from django.db.models import Count from django.db.models import Count
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -67,7 +69,7 @@ class ApplicationTreeNodeMixin:
i = root_node.id + '_' + category.value i = root_node.id + '_' + category.value
node = cls.create_choice_node( node = cls.create_choice_node(
category, i, pid=root_node.id, tp='category', category, i, pid=root_node.id, tp='category',
counts=counts, opened=True, show_empty=show_empty, counts=counts, opened=False, show_empty=show_empty,
show_count=show_count show_count=show_count
) )
if not node: if not node:
@ -84,7 +86,7 @@ class ApplicationTreeNodeMixin:
pid = root_node.id + '_' + category.value pid = root_node.id + '_' + category.value
i = root_node.id + '_' + tp.value i = root_node.id + '_' + tp.value
node = cls.create_choice_node( node = cls.create_choice_node(
tp, i, pid, tp='type', counts=counts, tp, i, pid, tp='type', counts=counts, opened=False,
show_empty=show_empty, show_count=show_count show_empty=show_empty, show_count=show_count
) )
if not node: if not node:
@ -94,18 +96,13 @@ class ApplicationTreeNodeMixin:
@staticmethod @staticmethod
def get_tree_node_counts(queryset): def get_tree_node_counts(queryset):
counts = {'applications': queryset.count()} counts = defaultdict(int)
category_counts = queryset.annotate(count=Count('id'))\ values = queryset.values_list('type', 'category')
.values('category', 'count') \ for i in values:
.order_by() tp = i[0]
for item in category_counts: category = i[1]
counts[item['category']] = item['count'] counts[tp] += 1
counts[category] += 1
type_counts = queryset.annotate(count=Count('id')) \
.values('type', 'count') \
.order_by()
for item in type_counts:
counts[item['type']] = item['count']
return counts return counts
@classmethod @classmethod

Loading…
Cancel
Save