0
我有我的自定义分页类。将上下文传递给Django Rest框架中的分页类
class BasicPagination(PageNumberPagination):
page_size = 3
page_size_query_param = 'page_size'
max_page_size = 20
def get_paginated_response(self, data):
has_next, has_previous = False, False
if self.get_next_link():
has_next = True
if self.get_previous_link():
has_previous = True
meta = collections.OrderedDict([
('page', self.page.number),
('has_next', has_next),
('has_previous', has_previous),
])
ret = collections.OrderedDict(meta=meta)
ret["results"] = data
return Response(ret)
另外我有一个generics.ListCreateAPIView
类,它有自定义查询集方法和pagination_class = BasicPagination
。我想通过self.kwargs.get("obj_type")
到分页类,以便它显示obj_type
而不是results
。这是我的班级观点。 如何将self.kwargs
传递给分页课程?
class Translation(ListCreateAPIView):
pagination_class = BasicPagination
serializer_class = TranslationStepSerializer
def get_queryset(self):
api_controller = ApiController.load()
obj_type = self.kwargs.get("obj_type")
pk = self.kwargs.get("pk")
data = api_controller.get_translation(obj_type, pk)
return data if not None else None
如果我明白你的意思,也许尝试重写BasicPagination构造函数接受obj_type的额外参数,然后返回super(PageNumberPa)。所以在你的查询集方法中,你可以重新初始化你的pagination_class并传递你的obj_type。 – pitaside