From fb949495386da570084bcbf7126d09b736c1e86c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 3 Apr 2020 11:34:42 +1100 Subject: [PATCH] Allow StockLocation filtering of null parent --- InvenTree/stock/api.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 86759a90aa..8e36e51ccf 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -19,7 +19,7 @@ from .serializers import LocationSerializer from .serializers import StockTrackingSerializer from InvenTree.views import TreeSerializer -from InvenTree.helpers import str2bool +from InvenTree.helpers import str2bool, isNull from InvenTree.status_codes import StockStatus import os @@ -223,9 +223,33 @@ class StockLocationList(generics.ListCreateAPIView): """ queryset = StockLocation.objects.all() - serializer_class = LocationSerializer + def get_queryset(self): + """ + Custom filtering: + - Allow filtering by "null" parent to retrieve top-level stock locations + """ + + queryset = super().get_queryset() + + loc_id = self.request.query_params.get('parent', None) + + if loc_id is not None: + + # Look for top-level locations + if isNull(loc_id): + queryset = queryset.filter(parent=None) + + else: + try: + loc_id = int(loc_id) + queryset = queryset.filter(parent=loc_id) + except ValueError: + pass + + return queryset + permission_classes = [ permissions.IsAuthenticated, ] @@ -237,7 +261,6 @@ class StockLocationList(generics.ListCreateAPIView): ] filter_fields = [ - 'parent', ] search_fields = [