Skip to main content

Cursor based pagination for Django - Forked and fixed for Python 3 by Boris Lapouga

Project description

Django cursor pagination for Python 3

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.
  • 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

p3-django-cursor-pagination-0.1.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

p3_django_cursor_pagination-0.1.5-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file p3-django-cursor-pagination-0.1.5.tar.gz.

File metadata

  • Download URL: p3-django-cursor-pagination-0.1.5.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for p3-django-cursor-pagination-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ec9a764fe36e4099a9c1dc379cb6dd7e1a3950fe60e226c5ce34ce70a530d3dc
MD5 fbd0023c54a97a1cbc3dd0b9ff1d7e6e
BLAKE2b-256 40dc33c025a5d5571d8f6dc3e692d98345060df7e1cfada3848167f8d6d13191

See more details on using hashes here.

File details

Details for the file p3_django_cursor_pagination-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: p3_django_cursor_pagination-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for p3_django_cursor_pagination-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1750243a0d854e5ddc5ef8618b484312136a0858e323cbe951c6a4f18548f83e
MD5 f4f519d0407909b4d5da5584bc54647a
BLAKE2b-256 15a1379fd2bc0a2987ad3dd9dd0e13a5e11dc42e59b2f6c8341ebd04fe578144

See more details on using hashes here.

Supported by

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