I am building an API using Django and Django rest framework and I am trying to add the page number on the json results. And I am trying to add the page number in the returned results for example, I am currently getting this;
{
"links": {
"next": null,
"previous": null
},
"count": 1,
"results": [
{
"url": "http://127.0.0.1:8000/api/brands/1/?format=json",
"name": "microsoft",
"description": "Nothing",
"top_brand": true,
"guid": "microsoft"
}
]
}
But I am trying to get something like this;
{
"count": 1,
"results": [
{
"url": "http://127.0.0.1:8000/api/brands/1/?format=json",
"name": "microsoft",
"description": "Nothing",
"top_brand": true,
"guid": "microsoft"
},
...
],
"page_number": 1,
"from": 1,
"to": 10,
"last_page": 4
}
The most important attribute is the page_number but I am not so sure how to get that from the parent class using self... I see no direct way besides using a function. Here is my code;
class StandardResultsSetPagination(PageNumberPagination):
page_size = 100
page_size_query_param = 'page_size'
max_page_size = 1000
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'count': self.page.paginator.count,
'results': data,
'page_number': self.page.paginator...,
})