Cursor based pagination for Django
Project description
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 a cursor is given and it does not refer to a valid object, the values of
has_previous
(forafter
) orhas_next
(forbefore
) will always returnTrue
. NULL
comes at the end in query results withORDER BY
both forASC
andDESC
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-cursor-pagination-0.2.1.tar.gz
.
File metadata
- Download URL: django-cursor-pagination-0.2.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76d64bd94c056096d549b27864a3515064a4115ccd8289ae718f1e538c386208 |
|
MD5 | e974eeba8e419f02be09cd37fcfeb65c |
|
BLAKE2b-256 | 15f3440595f30fe96870ac3f3ebc119782161cef2d6cc747f288740ac0076679 |
File details
Details for the file django_cursor_pagination-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: django_cursor_pagination-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ce63fe5ab45fd4d99e2fe5a722bd2ca5b09f1a3fc526b72a01e686b3b259024 |
|
MD5 | bc834ef0148face485a37d0c00b414a5 |
|
BLAKE2b-256 | 3883d5966841a6b7d0a1e2582c38e436910d3e4c0880f7c1a3f9bc353cabf675 |