Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrderingFilter has no attribute 'filter_queryset'

I have been trying to get OrderingFilter to work. From the example in django-rest-framework's page, it seems it should be fairly straightforward:

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ('username', 'email')

However, when I try to do that:

from django_filters.rest_framework import DjangoFilterBackend, FilterSet, OrderingFilter
...


class ListingViewSet(viewsets.ModelViewSet):
    queryset = Listing.objects.all()
    serializer_class = ListingSerializer
    filter_backends = (DjangoFilterBackend, OrderingFilter,)
    filter_class = ListFilter
    ordering_fields = ('price', 'views')
    ordering = ('price',)

I get an error about OrderingFilter not having a filter_queryset attribute:

ERROR 2016-11-13 03:14:57,304 log 25913 123145338523648 (<class 'AttributeError'>, AttributeError("'OrderingFilter' object has no attribute 'filter_queryset'",), <traceback object at 0x106ef48c8>)
Traceback (most recent call last):
  File "/.../lib/python3.5/site-packages/rest_framework/views.py", line 474, in dispatch
    response = handler(request, *args, **kwargs)
  File "/.../lib/python3.5/site-packages/rest_framework/mixins.py", line 40, in list
    queryset = self.filter_queryset(self.get_queryset())
  File "/.../lib/python3.5/site-packages/rest_framework/generics.py", line 151, in filter_queryset
    queryset = backend().filter_queryset(self.request, queryset, self)
AttributeError: 'OrderingFilter' object has no attribute 'filter_queryset'

Going into OrderingFilter, it indeed does not have a filter_queryset method, but the view expects it:

# in GenericAPIView(views.APIView)
def filter_queryset(self, queryset):
    """
    Given a queryset, filter it with whichever filter backend is in use.

    You are unlikely to want to override this method, although you may need
    to call it either from a list view, or from a custom `get_object`
    method if you want to apply the configured filtering backend to the
    default queryset.
    """
    for backend in list(self.filter_backends):
        queryset = backend().filter_queryset(self.request, queryset, self)
    return queryset

I have tried the various solutions in django rest framework queryset doesn't order, but it seems they do not work, either. I tried updating the most promising answer:

class YOUR_VIEW_SET(viewsets.ModelViewSet):
    #your code here
    ordering_filter = OrderingFilter()

    def filter_queryset(self, queryset):
        queryset = super(YOUR_VIEW_SET, self).filter_queryset(queryset)
        return self.ordering_filter.filter_queryset(self.request, queryset, self)

Since OrderingFilter does not have a filter_queryset method, I changed filter_queryset to filter (which it does have), but this call seems to clash with the signature of filter in OrderingFilter:

# In OrderingFilter(BaseCSVFilter, ChoiceFilter)
def filter(self, qs, value):
    if value in EMPTY_VALUES:
        return qs

    ordering = [self.get_ordering_value(param) for param in value]
    return qs.order_by(*ordering)

I have no idea what value in the function declaration is supposed to be so I could try to pass it to the function, but it seems qs refers to the queryset, so I assume that should be the first argument instead of the second, but again, I am not entirely sure what value would be; it doesn't seem to be request because it is not iterable.

I would appreciate any pointers or ideas, or workarounds, and I apologize if I missed something obvious; I am not terribly familiar with Django.

like image 502
Juan Torres Avatar asked Nov 13 '16 03:11

Juan Torres


1 Answers

Just ran into this myself!

Don't use the class from django_filters, use rest_framework.filters.OrderingFilter instead.

from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
...
like image 139
pnovotnak Avatar answered Oct 15 '22 15:10

pnovotnak