Filtering ListAPIView in django-rest-framework

后端 未结 2 1833
猫巷女王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:29

    I know is late for this, but I wrote a little app that extends for ListAPIView and do this easier, check it out:

    https://github.com/angvp/drf-lafv

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题