Code Monkey home page Code Monkey logo

django-cursor-pagination's Introduction

Django cursor pagination

A cursor based pagination system for Django. Instead of refering to specific pages by number, we give every item in the queryset a cursor based on its ordering values. We then ask for subsequent records by asking for records after the cursor of the last item we currently have. Similarly we can ask for records before the cursor of the first item to navigate back through the list.

This approach has two major advantages over traditional pagination. Firstly, it ensures that when new data is written into the table, records cannot be moved onto the next page. Secondly, it is much faster to query against the database as we are not using very large offset values.

There are some significant drawbacks over "traditional" pagination. The data must be ordered by some database field(s) which are unique across all records. A typical use case would be ordering by a creation timestamp and an id. It is also more difficult to get the range of possible pages for the data.

The inspiration for this project is largely taken from this post by David Cramer, and the connection spec for Relay GraphQL. Much of the implementation is inspired by Django rest framework's Cursor pagination.. The main difference between the Disqus approach and the one used here is that we require the ordering to be totally determinate instead of using offsets.

Installation

pip install django-cursor-pagination

Usage

from cursor_pagination import CursorPaginator

from myapp.models import Post


def posts_api(request, after=None):
    qs = Post.objects.all()
    page_size = 10
    paginator = CursorPaginator(qs, ordering=('-created', '-id'))
    page = paginator.page(first=page_size, after=after)
    data = {
        'objects': [serialize_page(p) for p in page],
        'has_next_page': page.has_next,
        'last_cursor': paginator.cursor(page[-1])
    }
    return data

Reverse pagination can be achieved by using the last and before arguments to paginator.page.

Caveats

  • The ordering specified must uniquely identify the object.
  • If there are multiple ordering fields, then they must all have the same direction
  • No support for multiple ordering fields in SQLite as it does not support tuple comparison.

django-cursor-pagination's People

Contributors

joealcorn avatar mjtamlyn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.