Allow StockLocation filtering of null parent

This commit is contained in:
Oliver Walters 2020-04-03 11:34:42 +11:00
parent f5150f549a
commit fb94949538

View File

@ -19,7 +19,7 @@ from .serializers import LocationSerializer
from .serializers import StockTrackingSerializer from .serializers import StockTrackingSerializer
from InvenTree.views import TreeSerializer from InvenTree.views import TreeSerializer
from InvenTree.helpers import str2bool from InvenTree.helpers import str2bool, isNull
from InvenTree.status_codes import StockStatus from InvenTree.status_codes import StockStatus
import os import os
@ -223,9 +223,33 @@ class StockLocationList(generics.ListCreateAPIView):
""" """
queryset = StockLocation.objects.all() queryset = StockLocation.objects.all()
serializer_class = LocationSerializer 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 = [ permission_classes = [
permissions.IsAuthenticated, permissions.IsAuthenticated,
] ]
@ -237,7 +261,6 @@ class StockLocationList(generics.ListCreateAPIView):
] ]
filter_fields = [ filter_fields = [
'parent',
] ]
search_fields = [ search_fields = [