diff --git a/InvenTree/InvenTree/filters.py b/InvenTree/InvenTree/filters.py new file mode 100644 index 0000000000..b7ee52fb5a --- /dev/null +++ b/InvenTree/InvenTree/filters.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from rest_framework.filters import OrderingFilter + + +class InvenTreeOrderingFilter(OrderingFilter): + """ + Custom OrderingFilter class which allows aliased filtering of related fields. + + To use, simply specify this filter in the "filter_backends" section. + + Then, you can specify aliasing for ordering fields (or use ordering_fields as normal), e.g. + + filter_backends = [ + InvenTreeOrderingFilter, + ] + + ordering_fields = [ + 'name', + 'quantity', + ('part__SKU', 'SKU') + ] + + Here, ordering by "SKU" will actually order by the "SKU" field on the related part field + + """ + + def get_ordering(self, request, queryset, view): + + ordering = super().get_ordering(request, queryset, view) + + print("ORDERING:", ordering) + return ordering \ No newline at end of file diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index a834989fd9..b4c0022604 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -7,11 +7,12 @@ from __future__ import unicode_literals from django.conf.urls import url, include -from django_filters.rest_framework import DjangoFilterBackend +from django_filters import rest_framework as rest_filters from rest_framework import generics from rest_framework import filters, status from rest_framework.response import Response +from InvenTree.filters import InvenTreeOrderingFilter from InvenTree.helpers import str2bool from InvenTree.api import AttachmentMixin from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus @@ -144,7 +145,7 @@ class POList(generics.ListCreateAPIView): return queryset filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter, ] @@ -226,9 +227,9 @@ class POLineItemList(generics.ListCreateAPIView): return self.serializer_class(*args, **kwargs) filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, filters.SearchFilter, - filters.OrderingFilter + InvenTreeOrderingFilter ] ordering_fields = [ @@ -272,7 +273,7 @@ class SOAttachmentList(generics.ListCreateAPIView, AttachmentMixin): serializer_class = SOAttachmentSerializer filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, ] filter_fields = [ @@ -396,7 +397,7 @@ class SOList(generics.ListCreateAPIView): return queryset filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter, ] @@ -495,7 +496,7 @@ class SOLineItemList(generics.ListCreateAPIView): return queryset filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter ] @@ -580,7 +581,7 @@ class SOAllocationList(generics.ListCreateAPIView): return queryset filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, ] # Default filterable fields @@ -598,7 +599,7 @@ class POAttachmentList(generics.ListCreateAPIView, AttachmentMixin): serializer_class = POAttachmentSerializer filter_backends = [ - DjangoFilterBackend, + rest_filters.DjangoFilterBackend, ] filter_fields = [