Browse Source

customiza ordenação dinâmica através do filtro

pull/2460/head
Leandro Roberto 6 years ago
parent
commit
f214b43f3c
  1. 11
      sapl/api/views.py

11
sapl/api/views.py

@ -9,6 +9,7 @@ from django.utils.decorators import classonlymethod
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
import django_filters import django_filters
from django_filters.filters import CharFilter
from django_filters.rest_framework.backends import DjangoFilterBackend from django_filters.rest_framework.backends import DjangoFilterBackend
from django_filters.rest_framework.filterset import FilterSet from django_filters.rest_framework.filterset import FilterSet
from django_filters.utils import resolve_field from django_filters.utils import resolve_field
@ -74,6 +75,9 @@ class SaplApiViewSetConstrutor(ModelViewSet):
# Define uma classe padrão para filtro caso não tenha sido # Define uma classe padrão para filtro caso não tenha sido
# criada a classe sapl.api.forms.{model}FilterSet # criada a classe sapl.api.forms.{model}FilterSet
class SaplFilterSet(FilterSet): class SaplFilterSet(FilterSet):
o = CharFilter(method='filter_o')
class Meta: class Meta:
model = _model model = _model
fields = '__all__' fields = '__all__'
@ -86,6 +90,13 @@ class SaplApiViewSetConstrutor(ModelViewSet):
}, },
} }
def filter_o(self, queryset, name, value):
try:
return queryset.order_by(
*map(str.strip, value.split(',')))
except:
return queryset
@classmethod @classmethod
def filter_for_field(cls, f, name, lookup_expr='exact'): def filter_for_field(cls, f, name, lookup_expr='exact'):
# Redefine método estático para ignorar filtro para # Redefine método estático para ignorar filtro para

Loading…
Cancel
Save