Filtering ListAPIView in django-rest-framework

后端 未结 2 1838
猫巷女王i
猫巷女王i 2021-02-01 18:41

I\'m using ListAPIView, but I can\'t filter the results. My code is:

class UserPostReadView(generics.ListAPIView):
    serializer_class = PostSerializer
    mode         


        
2条回答
  •  面向向阳花
    2021-02-01 19:36

    I've found the solution

    class UserPostsReadView(generics.ListAPIView):
        serializer_class = PostSerializer
        model = serializer_class.Meta.model
        paginate_by = 100
        def get_queryset(self):
            poster_id = self.kwargs['poster_id']
            queryset = self.model.objects.filter(poster_id=poster_id)
            return queryset.order_by('-post_time')
    

    Source: http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-the-url

提交回复
热议问题