搜索在Django Rest Framework中不起作用。

huangapple go评论53阅读模式
英文:

Searching is not working in django rest_framework

问题

搜索在Django Rest Framework中不起作用。搜索特定员工提交的请假申请的批准状态。但在管理员用户中,筛选/搜索/排序正常工作,但在员工端不起作用。

Views.py

class LeaveList(APIView):

    authentication_classes = [SessionAuthentication, BasicAuthentication, TokenAuthentication]
    permission_classes = [IsAuthenticated]  

    filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
    ordering_fields = ['apply_date', 'number_of_days', 'status']
    search_fields = ['status']
    filter_fields = ['status']

    def get(self, request, format=None):
        employee = request.user

        filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
        search_fields = ['status']

        queryset = LeaveApplicationModel.objects.filter(employee_id=employee.id)
        serializer_class = LeaveApplicationsListSerializer(queryset, many=True)

        return Response(data=serializer_class.data)

Serializers.py

class LeaveApplicationsListSerializer(serializers.ModelSerializer):
  
    class Meta:
        model = LeaveApplicationModel
        exclude = ['id', 'employee']

postman request
postman request

expecting

英文:

Searching is not working in the django rest framework. searching for the approved status of leave applied by a particular employee. But the filtering/ searching/ sorting is properly working in the admin user. but not in the employee side.

Views.py

class LeaveList(APIView):

    authentication_classes = [SessionAuthentication, BasicAuthentication, TokenAuthentication]
    permission_classes = [IsAuthenticated]  

    filter_backends = [DjangoFilterBackend,SearchFilter,OrderingFilter]
    ordering_fields = ['apply_date','number_of_days','status']
    search_fields = ['status']
    filter_fields = ['status']

    def get(self, request, format=None):
        employee = request.user

        filter_backends = [DjangoFilterBackend,SearchFilter,OrderingFilter]
        search_fields = ['status']

        queryset = LeaveApplicationModel.objects.filter(employee_id=employee.id)
        serializer_class = LeaveApplicationsListSerializer(queryset, many=True)

        return Response(data=serializer_class.data)

Serializers.py

class LeaveApplicationsListSerializer(serializers.ModelSerializer):
  
    class Meta:
        model = LeaveApplicationModel
        exclude = ['id', 'employee']

postman request
postman request

expecting

答案1

得分: 1

不要有别的内容,只返回翻译好的部分:

  • "You're not using the search_filters anywhere. GenericAPIView or any class that subclasses it, e.g. ListAPIView, will use it automatically. It's in the docs."

Chinese Translation: "您没有在任何地方使用search_filtersGenericAPIView或任何继承它的类,例如ListAPIView,将自动使用它。它在文档中有描述。"

  • "If I were you, I'd rather subclass ListAPIView and set the relevant available attributes correctly. You can overwrite get_queryset to filter for the employee:"

Chinese Translation: "如果我是您,我宁愿子类化ListAPIView,并正确设置相关的可用属性。您可以覆盖get_queryset以进行员工筛选:"

  • The Python code remains unchanged.

Python 代码部分保持不变。

英文:

You're not using the search_filters anywhere. GenericAPIView or any class that subclasses it, e.g. ListAPIView, will use it automatically. It's in the docs.

If I were you, I'd rather subclass ListAPIView and set the relevant available attributes correctly. You can overwrite get_queryset to filter for the employee:

class LeaveList(ListAPIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication, TokenAuthentication]
    permission_classes = [IsAuthenticated]  
    filter_backends = [DjangoFilterBackend,SearchFilter,OrderingFilter]
    ordering_fields = ['apply_date','number_of_days','status']
    search_fields = ['status']
    filter_fields = ['status']
    serializer_class = LeaveApplicationsListSerializer

    def get_queryset(self):
        employee = self.request.user

        return LeaveApplicationModel.objects.filter(employee_id=employee.id)

huangapple
  • 本文由 发表于 2023年2月16日 18:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定