Django Rest Framework SearchFilter不起作用

时间:2018-06-08 13:46:17

标签: python django django-rest-framework

我正在尝试在ViewSetOrderingFilterSearchFilter)中实现两个过滤器,但只有OrderingFilter有效。我可以在browsableAPI中看到OrderingFilter的正确控件,但我没有得到适当的搜索控件。

这是我的ViewSet

from rest_framework.filters import OrderingFilter, SearchFilter
from GeneralApp import serializers, models

class CountryViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = models.Country.objects.all()
    serializer_class = serializers.CountrySerializer
    filter_backends = (OrderingFilter, SearchFilter)
    search_fields = ('name', 'code', 'calling_code')

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试在SearchFilter之后添加逗号我认为这是问题

filter_backends = (OrderingFilter, SearchFilter,)

答案 1 :(得分:0)

你必须定义 search_fields或filter_class:

search_fields = ('name', 'continent', 'language')
filter_class  = MyOwnClass