mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Reduced tree time with some better queries
This commit is contained in:
parent
b519a1981d
commit
037dc6a0d6
@ -52,8 +52,10 @@ class TreeSerializer(views.APIView):
|
|||||||
if item.has_children:
|
if item.has_children:
|
||||||
nodes = []
|
nodes = []
|
||||||
|
|
||||||
|
"""
|
||||||
for child in item.children.all().order_by('name'):
|
for child in item.children.all().order_by('name'):
|
||||||
nodes.append(self.itemToJson(child))
|
nodes.append(self.itemToJson(child))
|
||||||
|
"""
|
||||||
|
|
||||||
data['nodes'] = nodes
|
data['nodes'] = nodes
|
||||||
|
|
||||||
@ -63,10 +65,12 @@ class TreeSerializer(views.APIView):
|
|||||||
|
|
||||||
return self.model.objects.all()
|
return self.model.objects.all()
|
||||||
|
|
||||||
def generate_tree(self, items):
|
def generate_tree(self):
|
||||||
|
|
||||||
nodes = []
|
nodes = []
|
||||||
|
|
||||||
|
items = self.get_items()
|
||||||
|
|
||||||
# Construct the top-level items
|
# Construct the top-level items
|
||||||
top_items = [i for i in items if i.parent is None]
|
top_items = [i for i in items if i.parent is None]
|
||||||
|
|
||||||
@ -87,9 +91,7 @@ class TreeSerializer(views.APIView):
|
|||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
""" Respond to a GET request for the Tree """
|
""" Respond to a GET request for the Tree """
|
||||||
|
|
||||||
items = self.model.objects.all()
|
self.generate_tree()
|
||||||
|
|
||||||
self.generate_tree(items)
|
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
'tree': [self.tree]
|
'tree': [self.tree]
|
||||||
|
@ -34,6 +34,11 @@ class PartCategoryTree(TreeSerializer):
|
|||||||
def root_url(self):
|
def root_url(self):
|
||||||
return reverse('part-index')
|
return reverse('part-index')
|
||||||
|
|
||||||
|
def get_items(self):
|
||||||
|
|
||||||
|
print("hello world")
|
||||||
|
return PartCategory.objects.all().prefetch_related('parts', 'children')
|
||||||
|
|
||||||
|
|
||||||
class CategoryList(generics.ListCreateAPIView):
|
class CategoryList(generics.ListCreateAPIView):
|
||||||
""" API endpoint for accessing a list of PartCategory objects.
|
""" API endpoint for accessing a list of PartCategory objects.
|
||||||
|
@ -69,13 +69,20 @@ class PartCategory(InvenTreeTree):
|
|||||||
return self.partcount
|
return self.partcount
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def partcount(self):
|
def partcount(self, cascade=True, active=True):
|
||||||
""" Return the total part count under this category
|
""" Return the total part count under this category
|
||||||
(including children of child categories)
|
(including children of child categories)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return len(Part.objects.filter(category__in=self.getUniqueChildren(),
|
if cascade:
|
||||||
active=True))
|
query = Part.objects.filter(category__in=self.getUniqueChildren())
|
||||||
|
else:
|
||||||
|
query = Part.objects.filter(category=self)
|
||||||
|
|
||||||
|
if active:
|
||||||
|
query = query.filter(active=True)
|
||||||
|
|
||||||
|
return query.count()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_parts(self):
|
def has_parts(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user