28

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

class UserPostReadView(generics.ListAPIView):
    serializer_class = PostSerializer
    model = serializer_class.Meta.model
    queryset = model.objects.order_by('-post_time')
    lookup_field = 'poster_id'
    paginate_by = 100

In this case, lookup_field is ignored, but the documentation says that it's supported for this class too. If I try to implement a custom get over a generic view, I don't know how to reimplement paginate_by. Any ideas?

2 Answers 2

43

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

4

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

2
  • Try to include a specific example of how you would use the app, don't just link to it. It looks deprecated now though. Commented Aug 8, 2023 at 13:50
  • Oh back in 2015 I thought the link to the repo that had an example in the README was enough, sorry. Commented Aug 15, 2023 at 19:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.