0

I have a simple nested router using drf-nested-routers, similar to the example on the readme page. The list view on the nested route does not paginate at all, ignoring my DEFAULT_PAGINATION_CLASS setting. Is this by design? Do nested routes have to manually implement pagination? If I try to call self.get_paginated_response in my nested viewset's list method, I get this error:

AttributeError at /api/foo/13/bar/
'PageNumberPagination' object has no attribute 'page'

Here's my list method in my nested view:

def list(self, request, workplan_pk=None):
        milestones = self.get_queryset()
        wp = get_object_or_404(Workplan, pk=workplan_pk)
        milestones = milestones.filter(workplan=wp)
        return Response(self.get_serializer_class()(milestones, many=True, context={'request': request}).data)
Neil
  • 7,042
  • 9
  • 43
  • 78
  • I had nested routers pagination working, no problems. Pagination is handled by views which use a paginator, views know nothing about routing. They only get the request as a parameter. I think the problem is somewhere else. – Ivan Oct 22 '15 at 16:24
  • Could you post the complete stack trace? – Ivan Oct 22 '15 at 16:27
  • @Ivan what does your nested router's `list` method return? Mine returns a DRF Response object with serialized data. I'm wondering if I should be returning something else. I've updated the question to show my `list` method – Neil Oct 22 '15 at 17:47
  • Mine returned serialized paginated data. In your list you do not paginate data at all. I will post an answer. – Ivan Oct 22 '15 at 17:52

1 Answers1

1

This has nothing to do with routers. Routing is transparent to views, and the only thing they get is a Request object.

You can override ModelViewSet.get_queryset() like this:

class WorkplanMilestones(ModelViewSet):
    #...
    def get_queryset(self):
        wp = get_object_or_404(Workplan, pk=self.kwargs['workplan_pk'])
        return wp.milestones

I am assuming here that the url parameter is called workplan_pk and milestones is the reverse relationship for the milestone model.

This will return workplan's milestones and the rest (including pagination) is handled by ModelViewSet.

Ivan
  • 5,803
  • 2
  • 29
  • 46
  • I understand that this should not have anything to do with routers. On the other hand if my list method does not paginate the data explicitly, where will the pagination happen? I understand from drf-nested-routers docs that I have to overwrite `list`, so don't I need to paginate there explicitly? – Neil Oct 22 '15 at 18:01
  • I will add a complete example. – Ivan Oct 22 '15 at 18:06
  • 1
    @Neil the pagination happens in `list`, so if you are overriding it you will also have to handle the pagination. If you only override `get_queryset` though, `list` will do the pagination for you. – Ivan Oct 22 '15 at 19:02