mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Implements custom filtering back end
This commit is contained in:
parent
c9756d30bd
commit
4b8ef2ad62
@ -10,25 +10,42 @@ class InvenTreeOrderingFilter(OrderingFilter):
|
||||
|
||||
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
|
||||
Then, specify a ordering_field_aliases attribute:
|
||||
|
||||
ordering_field_alises = {
|
||||
'name': 'part__part__name',
|
||||
'SKU': 'part__SKU',
|
||||
}
|
||||
"""
|
||||
|
||||
def get_ordering(self, request, queryset, view):
|
||||
|
||||
ordering = super().get_ordering(request, queryset, view)
|
||||
|
||||
print("ORDERING:", ordering)
|
||||
return ordering
|
||||
aliases = getattr(view, 'ordering_field_aliases', None)
|
||||
|
||||
# Attempt to map ordering fields based on provided aliases
|
||||
if ordering is not None and aliases is not None:
|
||||
"""
|
||||
Ordering fields should be mapped to separate fields
|
||||
"""
|
||||
|
||||
for idx, field in enumerate(ordering):
|
||||
|
||||
reverse = False
|
||||
|
||||
if field.startswith('-'):
|
||||
field = field[1:]
|
||||
reverse = True
|
||||
|
||||
if field in aliases:
|
||||
ordering[idx] = aliases[field]
|
||||
|
||||
if reverse:
|
||||
ordering[idx] = '-' + ordering[idx]
|
||||
|
||||
return ordering
|
||||
|
@ -232,10 +232,16 @@ class POLineItemList(generics.ListCreateAPIView):
|
||||
InvenTreeOrderingFilter
|
||||
]
|
||||
|
||||
ordering_field_aliases = {
|
||||
'MPN': 'part__manufacturer_part__MPN',
|
||||
'SKU': 'part__SKU',
|
||||
'part_name': 'part__part__name',
|
||||
}
|
||||
|
||||
ordering_fields = [
|
||||
'part__part__name',
|
||||
'part__MPN',
|
||||
'part__SKU',
|
||||
'part_name',
|
||||
'MPN',
|
||||
'SKU',
|
||||
'reference',
|
||||
'quantity',
|
||||
'received',
|
||||
|
Loading…
Reference in New Issue
Block a user