12👍
✅
I would suggest implementing this using a dedicated Pagination class.
class PaginationWithAggregates(pagination.LimitOffsetPagination):
def paginate_queryset(self, queryset, request, view=None):
self.total_duration = queryset.aggregate(total_duration=Sum('duration'))['total_duration']
return super(PaginationWithAggregates, self).paginate_queryset(queryset, request, view)
def get_paginated_response(self, data):
paginated_response = super(PaginationWithAggregates, self).get_paginated_response(data)
paginated_response.data['total_duration'] = self.total_duration
return paginated_response
Don’t forget to declare this pagination class on your GenericView.
Source:stackexchange.com