Skip to main content

Cursor based pagination for Django

Project description

Django cursor pagination Build Status

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.
  • If a cursor is given and it does not refer to a valid object, the values of has_previous (for after) or has_next (for before) will always return True.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-cursor-pagination-0.1.4.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

django_cursor_pagination-0.1.4-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django-cursor-pagination-0.1.4.tar.gz.

File metadata

  • Download URL: django-cursor-pagination-0.1.4.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for django-cursor-pagination-0.1.4.tar.gz
Algorithm Hash digest
SHA256 063fce867442c97798a247a306f9d889374eb630a3246b15493f08b72d3dbcd7
MD5 c409268f184c9a299c5d5d335805adf2
BLAKE2b-256 e9142f5e3818cac19edd23aec4e40f6ea33fd4cc00d4b42b094ece3515b9e5e2

See more details on using hashes here.

File details

Details for the file django_cursor_pagination-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_cursor_pagination-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for django_cursor_pagination-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0a738fb24d1e52b28556054804a7cb6cc76c2c32019299d3321fc1c77be49c73
MD5 46d897d6cb0eaa7045a981dda1f269a1
BLAKE2b-256 9f4bc86137e613c9eedfb360a04697dc0a961c9b001b63e520ccdb61e41ce89c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page