In filters.py, I have a CustomFilter
defined that has values of type ComboSortFilter
and IntegerListFilter
.
In views.py, I defined a ViewSet that has filter_class = CustomFilter
and this works great for GET
requests, using query-string params in the url, that get processed using the custom filtering.
I also need to support POST
requests, because some of the filter criteria is too long to put in the URL.
So I added a post
method to my ViewSet where I pull the params from request.DATA
then filter, serialize and paginate them...
in my ViewSet's post method in views.py:
queryset = MyModel.objects.filter(**filter_args)
page = self.paginate_queryset(queryset)
serializer = self.get_pagination_serializer(page)
return Response(serializer.data)
For simple filtering the above works fine. However, the filtering that I have defined in my CustomFilter
that filters fields using ComboSortFilter
and IntegerListFilter
is a bit more complex:
in filters.py:
class IntegerListFilter(django_filters.Filter):
def filter(self, qs, value):
if value not in (None, ''):
integers = [int(v) for v in value.split(',')]
return qs.filter(**{'{0}__{1}'.format(self.name, self.lookup_type): integers})
return qs
class ComboSortFilter(django_filters.Filter):
def __init__(self, threshold, lookup_type, order='ASC'):
super(ComboSortFilter, self).__init__(lookup_type=lookup_type)
self.threshold = threshold
self.order = order
def filter(self, qs, value):
if value not in (None, ''):
fields = [str(v) for v in value.split(',')]
for field in fields: # filters each field's values relative to threshold
qs = qs.filter(**{'{0}__{1}'.format(field, self.lookup_type): self.threshold})
if self.order == 'DESC':
fields = ['-{0}'.format(f) for f in fields]
qs = qs.order_by(*fields)
return qs
class CustomFilter(django_filters.FilterSet):
thing_ids = IntegerListFilter(name="thing_id", lookup_type='in')
sort_desc = ComboSortFilter(lookup_type='gte', threshold=100, order='DESC')
sort_asc = ComboSortFilter(lookup_type='lte', threshold=100)
Is there any way to reference the CustomFilter
FilterSet or either of my two custom filters from within the ViewSet's post method? I would hate to have to duplicate all this filtering code in views.py
to get it to work.
All the documentation on Django-rest-framework:filters only includes simple examples either rolling your own in the views, or using a filter class. Is there a way to call a filter class directly from a view, overriding the values it normally gets from the querystrings?
figured it out:
class MyPostViewSet(BaseModelViewSet):
def post(self, request, *args, **kwargs):
queryset = self.queryset
filter_params = request.DATA or request.GET
if filter_params:
queryfilter = self.filter_class(filter_params, queryset=queryset)
queryset = queryfilter.qs
page = self.paginate_queryset(queryset)
serializer = self.get_pagination_serializer(page)
return Response(serializer.data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With