mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add a custom OrderingFilter class
Needs further work
This commit is contained in:
parent
2923589c4a
commit
c9756d30bd
34
InvenTree/InvenTree/filters.py
Normal file
34
InvenTree/InvenTree/filters.py
Normal file
@ -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
|
@ -7,11 +7,12 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.conf.urls import url, include
|
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 generics
|
||||||
from rest_framework import filters, status
|
from rest_framework import filters, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
from InvenTree.filters import InvenTreeOrderingFilter
|
||||||
from InvenTree.helpers import str2bool
|
from InvenTree.helpers import str2bool
|
||||||
from InvenTree.api import AttachmentMixin
|
from InvenTree.api import AttachmentMixin
|
||||||
from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus
|
from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus
|
||||||
@ -144,7 +145,7 @@ class POList(generics.ListCreateAPIView):
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
filters.SearchFilter,
|
filters.SearchFilter,
|
||||||
filters.OrderingFilter,
|
filters.OrderingFilter,
|
||||||
]
|
]
|
||||||
@ -226,9 +227,9 @@ class POLineItemList(generics.ListCreateAPIView):
|
|||||||
return self.serializer_class(*args, **kwargs)
|
return self.serializer_class(*args, **kwargs)
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
filters.SearchFilter,
|
filters.SearchFilter,
|
||||||
filters.OrderingFilter
|
InvenTreeOrderingFilter
|
||||||
]
|
]
|
||||||
|
|
||||||
ordering_fields = [
|
ordering_fields = [
|
||||||
@ -272,7 +273,7 @@ class SOAttachmentList(generics.ListCreateAPIView, AttachmentMixin):
|
|||||||
serializer_class = SOAttachmentSerializer
|
serializer_class = SOAttachmentSerializer
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
]
|
]
|
||||||
|
|
||||||
filter_fields = [
|
filter_fields = [
|
||||||
@ -396,7 +397,7 @@ class SOList(generics.ListCreateAPIView):
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
filters.SearchFilter,
|
filters.SearchFilter,
|
||||||
filters.OrderingFilter,
|
filters.OrderingFilter,
|
||||||
]
|
]
|
||||||
@ -495,7 +496,7 @@ class SOLineItemList(generics.ListCreateAPIView):
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
filters.SearchFilter,
|
filters.SearchFilter,
|
||||||
filters.OrderingFilter
|
filters.OrderingFilter
|
||||||
]
|
]
|
||||||
@ -580,7 +581,7 @@ class SOAllocationList(generics.ListCreateAPIView):
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Default filterable fields
|
# Default filterable fields
|
||||||
@ -598,7 +599,7 @@ class POAttachmentList(generics.ListCreateAPIView, AttachmentMixin):
|
|||||||
serializer_class = POAttachmentSerializer
|
serializer_class = POAttachmentSerializer
|
||||||
|
|
||||||
filter_backends = [
|
filter_backends = [
|
||||||
DjangoFilterBackend,
|
rest_filters.DjangoFilterBackend,
|
||||||
]
|
]
|
||||||
|
|
||||||
filter_fields = [
|
filter_fields = [
|
||||||
|
Loading…
Reference in New Issue
Block a user