英文:
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
英文:
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']
答案1
得分: 1
不要有别的内容,只返回翻译好的部分:
- "You're not using the 
search_filtersanywhere.GenericAPIViewor any class that subclasses it, e.g.ListAPIView, will use it automatically. It's in the docs." 
Chinese Translation: "您没有在任何地方使用search_filters。GenericAPIView或任何继承它的类,例如ListAPIView,将自动使用它。它在文档中有描述。"
- "If I were you, I'd rather subclass 
ListAPIViewand set the relevant available attributes correctly. You can overwriteget_querysetto 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论