Support pagination natively
See original GitHub issueWhat do you guys think about native support to pagination? We could implement that using Django paginator (for example) since it doesn’t have external dependencies.
That’s helpful in most listing APIs. Here is just an example on how it could be implemented in the Resource:
DEFAULT_RESULTS_PER_PAGE = 20
class Resource(object):
...
def serialize_list(self, data):
if getattr(self, 'paginate', False):
page_size = getattr(self, 'page_size', DEFAULT_RESULTS_PER_PAGE)
paginator = Paginator(data, page_size)
try:
page_number = int(self.request.GET.get('p', 1))
except ValueError:
page_number = None
if page_number not in paginator.page_range:
raise BadRequest('Invalid page number')
self.page = paginator.page(page_number)
data = self.page.object_list
return super().serialize_list(data)
def wrap_list_response(self, data):
response_dict = super().wrap_list_response(data)
if hasattr(self, 'page'):
if self.page.has_next():
next_page = self.page.next_page_number()
else:
next_page = None
if self.page.has_previous():
previous_page = self.page.previous_page_number()
else:
previous_page = None
response_dict['pagination'] = {
'num_pages': self.page.paginator.num_pages,
'count': self.page.paginator.count,
'page': self.page.number,
'start_index': self.page.start_index(),
'end_index': self.page.end_index(),
'next_page': next_page,
'previous_page': previous_page,
}
return response_dict
And to use simply add to your resource:
MyResource(Resource):
paginate = True
page_size = 50 # optional
If that seems useful I’m willing to create a PR with tests and docs.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Developers - Support pagination natively - - Bountysource
Coming soon: A brand new website interface for an even better experience!
Read more >Spring Data and Native Query with pagination - Stack Overflow
In a web project, using latest spring-data (1.10. 2) with a MySQL 5.6 database, I'm trying to use a native query with pagination...
Read more >How to use paginated APIs - SyncWith
Page-based pagination iterates through pages of fixed size starting from page 1, going to page 2, and so on until all pages are...
Read more >Step by step guide to pagination in GraphQL - Daily.dev
GraphQL does not natively support count. It is the GraphQL platform that provides support for the count functionality. ... See _aggregate is added ......
Read more >Pagination in a Spring Boot REST API - DEV Community
Spring Boot provides many features to support Pagination natively. However, the PageImpl class is not directly usable with a RestTemplate ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @CalebeGeazi. Honestly I had forgot that this issue was open. Thanks for bumping it up.
@toastdriven do you have any thoughts about that? If you don’t oppose I think it’s time to add a few extra features here 😉
I’ve merged #114 witch adds pagination for Django. @Marcelo-Theodoro could you create a new PR that actually adds the pagination independent of framework?
Thanks!