python - Pass context to pagination class in Django Rest Framework -
i have custom pagination class.
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) also have generics.listcreateapiview class, has custom queryset method , pagination_class = basicpagination. wanna pass self.kwargs.get("obj_type") pagination class displays obj_type not results. here class view. how can pass self.kwargs pagination class?
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
i assuming -
it displays obj_type not results
you mean want key in response obj_type instead of "results". obj_type string in code.
i had similar requirement wanted modify response based on conditions. workaround, added required parameters data itself, through customised paginated response.
def get_paginated_response(self, data): if self.get_next_link(): next_page = data["page_no"] + 1 else: next_page = 0 response = { "next": next_page, 'count': self.page.paginator.count, 'cards': data["cards"], 'companies': data["companies"], 'positions': data["positions"], 'cities':data["cities"] } tags = data.get('tags', none) if tags not none: response['tags'] = tags return response(data=response) in case can like:
ret[data['obj_type']] = data['results'] and prior this, in queryset:
data = {'results': api_controller.get_translation(obj_type, pk), 'obj_type': obj_type}
Comments
Post a Comment