Skip to main content
Django cursor pagination [![Build Status](https://travis-ci.org/photocrowd/django-cursor-pagination.svg?branch=master)](https://travis-ci.org/photocrowd/django-cursor-pagination)
========================

THIS IS A FORK
========================
This is a fork of django-cursor-pagination, you probably don't want to use it.
Try photocrowd/django-cursor-pagination instead.

Original Readme
========================
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](http://cra.mr/2011/03/08/building-cursors-for-the-disqus-api) by David
Cramer, and the connection spec for [Relay
GraphQL](https://facebook.github.io/relay/graphql/connections.htm). Much of the
implementation is inspired by [Django rest framework's Cursor
pagination.](https://github.com/tomchristie/django-rest-framework/blob/9b56dda91850a07cfaecbe972e0f586434b965c3/rest_framework/pagination.py#L407-L707).
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
-----

```python
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`.


Metadata

Release files for django-cursor-pagination-dtkav 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cursor-pagination-dtkav 0.1.7
File Size Uploaded
django-cursor-pagination-dtkav-0.1.7.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cursor-pagination-dtkav 0.1.7
File Interpreter ABI Platform
django_cursor_pagination_dtkav-0.1.7-py2-none-any.whl Python 2 none any Details

Total release size: 12.3 kB

Release files / django-cursor-pagination-dtkav-0.1.7.tar.gz

Download URL django-cursor-pagination-dtkav-0.1.7.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5a3703277fd14021ca0282da6a838c95dac724de8ae3cc095ddf942a810bb401
BLAKE2b-256 checksum
How to use checksums
de3b4110719238c459535cec80b21993780bd3a95567c3cec2843456f82d2cb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.15rc1

Release files / django_cursor_pagination_dtkav-0.1.7-py2-none-any.whl

Download URL django_cursor_pagination_dtkav-0.1.7-py2-none-any.whl
Size 7.5 kB
Tags Python 2
SHA-256 checksum
How to use checksums
cac4b3a69c82b3eab7e0810359bb5f06d5dee214600cfddf7c365e8a352ee831
BLAKE2b-256 checksum
How to use checksums
24434047abbbe6742d8a6f1079abc43c56d958c933c297b258194406ec91e4ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.15rc1

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page