Django Filter Backend在Django的Restful API中不起作用

时间:2019-01-08 10:14:29

标签: django-rest-framework

我试图在使用params的url中进行搜索查询时使用django过滤器

view.py

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter
from .models import RecruiterRegister
from .serializers import RecruiterRegisterSerializer
# from rest_framework import filters

from rest_framework import generics

class RecruiterRegisterList(APIView):

    queryset=RecruiterRegister.objects.all()
    filter_backends = (DjangoFilterBackend,SearchFilter ,)
    filter_fields = ('email','password')
    search_fields = ('email','password')
    def get(self, request, format=None):
        recruiterRegisters = RecruiterRegister.objects.all()
        # self.filter_backends = (DjangoFilterBackend,)
        # self.filter_fields = ('password', )
        serializer = RecruiterRegisterSerializer(recruiterRegisters, many=True)
        return Response(serializer.data)


    def post(self, request, format=None):
        serializer = RecruiterRegisterSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        recruiterRegisters = self.get_object()
        recruiterRegisters.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

但是我无法获取经过过滤的数据。所有的api数据都会出现。

网址示例     http://127.0.0.1:8000/recruiterRegister/?password=random2

编辑的RecruiterRegisterList类:

class RecruiterRegisterList(generics.ListAPIView):

    queryset=RecruiterRegister.objects.all()
    serializer_class = RecruiterRegisterSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields=('email','password')
    # search_fields = ('email','password')

    def list(self, request ):
        queryset = self.get_queryset()
        serializer = RecruiterRegisterSerializer(queryset, many=True)
        return Response(serializer.data)

2 个答案:

答案 0 :(得分:1)

我仅添加以下行且工作正常:

filter_backends = self.filter_queryset(queryset)

您的代码如下:

class RecruiterRegisterList(generics.ListAPIView):

queryset=RecruiterRegister.objects.all()
serializer_class = RecruiterRegisterSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields=('email','password')
search_fields = ('email','password')

def list(self, request ):
    queryset = self.get_queryset()
    filter_backends = self.filter_queryset(queryset)
    serializer = RecruiterRegisterSerializer(filter_backends, many=True)
    return Response(serializer.data)

答案 1 :(得分:0)

第一个问题是-为什么要使用密码进行过滤或搜索?这是非常不健康和不安全的设计。密码不是以原始格式存储,而是经过哈希处理,因此过滤器永远不会被击中

现在,即使您确实想这样做,也可以在filter_backends中使用GenericAPIView进行过滤,而在APIView中则不可用,因此您可能要使用{{1} }来自ListAPIview

最后,即使您使用了rest_framework.generics,它仍然无法工作,因为您要覆盖ListAPIView方法而不是调用get

使用self.filter_queryset()并使用已经实现的ListAPIView方法,它将起作用