我一直在尝试让OrderingFilter工作。从django-rest-framework页面的示例中,看起来应该很简单:

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

但是,当我尝试这样做时:
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',)

我收到有关OrderingFilter没有filter_queryset属性的错误:
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'

进入OrderingFilter,它确实没有filter_queryset方法,但是 View 期望它:
# 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

我已经尝试了django rest framework queryset doesn't order中的各种解决方案,但似乎它们也不起作用。我尝试更新最有前途的答案:
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)

由于OrderingFilter没有filter_queryset方法,因此我将filter_queryset更改为filter(它确实具有),但是此调用似乎与filter中的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)

我不知道函数声明中的value应该是什么,所以我可以尝试将其传递给函数,但是qs似乎指向queryset,因此我假设应该将其作为第一个参数,而不是第二个参数,但是再次,我不完全知道value是什么;它似乎不是请求,因为它不是可迭代的。

如果有任何指示,想法或解决方法,我将不胜感激,如果我错过了明显的内容,我深表歉意。我对Django并不是很熟悉。

最佳答案

自己碰到这个!

不要使用django_filters中的类,而应使用rest_framework.filters.OrderingFilter

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

关于python - OrderingFilter没有属性 'filter_queryset',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40570311/

10-11 07:04