Allow PartCategory filtering by null parent

This commit is contained in:
Oliver Walters 2020-04-03 09:25:58 +11:00
parent 7e8664a4dd
commit d17056820b

View File

@ -57,6 +57,30 @@ class CategoryList(generics.ListCreateAPIView):
permissions.IsAuthenticated, permissions.IsAuthenticated,
] ]
def get_queryset(self):
"""
Custom filtering:
- Allow filtering by "null" parent to retrieve top-level part categories
"""
cat_id = self.request.query_params.get('parent', None)
queryset = super().get_queryset()
if cat_id is not None:
# Integer id?
try:
cat_id = int(cat_id)
queryset = queryset.filter(parent=cat_id)
except ValueError:
# Look for top-level categories?
if str(cat_id).lower() in ['top', 'null', 'none', 'false', '-1']:
queryset = queryset.filter(parent=None)
return queryset
filter_backends = [ filter_backends = [
DjangoFilterBackend, DjangoFilterBackend,
filters.SearchFilter, filters.SearchFilter,
@ -64,7 +88,6 @@ class CategoryList(generics.ListCreateAPIView):
] ]
filter_fields = [ filter_fields = [
'parent',
] ]
ordering_fields = [ ordering_fields = [