2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-20 05:46:34 +00:00

Part API changes

- Allow filtering parts with null parent (top-level category parts)
- Option to include sub-category parts or not
This commit is contained in:
Oliver Walters
2020-04-03 09:37:03 +11:00
parent 6e65a736e7
commit f5150f549a
2 changed files with 23 additions and 6 deletions

View File

@ -243,12 +243,25 @@ class PartList(generics.ListCreateAPIView):
# Start with all objects
parts_list = Part.objects.all()
if cat_id:
try:
category = PartCategory.objects.get(pk=cat_id)
parts_list = parts_list.filter(category__in=category.getUniqueChildren())
except PartCategory.DoesNotExist:
pass
cascade = str2bool(self.request.query_params.get('cascade', False))
if cat_id is not None:
if isNull(cat_id):
parts_list = parts_list.filter(category=None)
else:
try:
cat_id = int(cat_id)
category = PartCategory.objects.get(pk=cat_id)
# If '?cascade=true' then include parts which exist in sub-categories
if cascade:
parts_list = parts_list.filter(category__in=category.getUniqueChildren())
# Just return parts directly in the requested category
else:
parts_list = parts_list.filter(category=cat_id)
except (ValueError, PartCategory.DoesNotExist):
pass
# Ensure that related models are pre-loaded to reduce DB trips
parts_list = self.get_serializer_class().setup_eager_loading(parts_list)