question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support pagination natively

See original GitHub issue

What 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:open
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
seocamcommented, Sep 4, 2018

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 😉

3reactions
seocamcommented, Jan 15, 2019

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found