From e1c6ad77619900c9281e4024c46b3259ce4468cc Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 11 Apr 2020 22:57:16 +1000 Subject: [PATCH] Logic fix for part filtering --- InvenTree/part/api.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 47f88fe517..5f6eed41ac 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -149,6 +149,13 @@ class PartList(generics.ListCreateAPIView): - GET: Return list of objects - POST: Create a new Part object + + The Part object list can be filtered by: + - category: Filter by PartCategory reference + - cascade: If true, include parts from sub-categories + - is_template: Is the part a template part? + - variant_of: Filter by variant_of Part reference + - assembly: Filter by assembly field """ serializer_class = part_serializers.PartSerializer @@ -291,23 +298,23 @@ class PartList(generics.ListCreateAPIView): cascade = str2bool(self.request.query_params.get('cascade', False)) - if cat_id is not None: - - if isNull(cat_id): + if cat_id is None: + # Top-level parts + if not cascade: 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 + else: + try: + 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)