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)
4

1 回答 1

1

这与路由器无关。路由对视图是透明的,它们得到的唯一东西就是一个Request对象。

您可以像这样覆盖ModelViewSet.get_queryset()

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

我在这里假设调用了 url 参数workplan_pk并且milestones是里程碑模型的反向关系。

这将返回工作计划的里程碑,其余的(包括分页)由ModelViewSet.

于 2015-10-22T17:56:49.000 回答