diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 2667352515..0238a62c63 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -289,20 +289,35 @@ class StockLocationList(generics.ListCreateAPIView): queryset = super().get_queryset() - loc_id = self.request.query_params.get('parent', None) + params = self.request.query_params - if loc_id is not None: + cascade = str2bool(params.get('cascade', False)) - # Look for top-level locations - if isNull(loc_id): + loc_id = params.get('parent', None) + + # Look for top-level locations + if isNull(loc_id): + + # If we allow "cascade" at the top-level, this essentially means *all* locations + if not cascade: queryset = queryset.filter(parent=None) - - else: - try: - loc_id = int(loc_id) - queryset = queryset.filter(parent=loc_id) - except ValueError: - pass + + else: + + try: + location = StockLocation.objects.get(pk=loc_id) + + # All sub-locations to be returned too? + if cascade: + parents = location.get_descendants(include_self=True) + parent_ids = [p.id for p in parents] + queryset = queryset.filter(parent__in=parent_ids) + + else: + queryset = queryset.filter(parent=location) + + except (ValueError, StockLocation.DoesNotExist): + pass return queryset diff --git a/InvenTree/stock/templates/stock/sublocation.html b/InvenTree/stock/templates/stock/sublocation.html index b8b32328a2..c5a6062064 100644 --- a/InvenTree/stock/templates/stock/sublocation.html +++ b/InvenTree/stock/templates/stock/sublocation.html @@ -15,7 +15,16 @@