Skip to main content

Keyset Pagination (seek method) for django.

Project description

# Keyset Pagination for Django.

Django pagination uses the LIMIT/OFFSET method. This is fine for smaller offsets, but once you start getting beyond a few pages, it can perform really badly. This is because the database needs to fetch all of the previous rows, even though it discards them.

Using Keyset Pagination allows for better performing "next page" fetches, at the cost of not being able to randomly fetch a page. That is, if you know the last element from page N-1, then you may fetch page N, but otherwise you really can't.

Keyset Pagination, sometimes called the Seek Method, has been documented by [Markus Winand](https://use-the-index-luke.com/sql/partial-results/fetch-next-page) and [Joe Nelson](https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/). If you are not familiar with the concept, I strongly suggest you read the articles above.

In order to use the paginator within this package, you will probably also need to use the provided View mixin: this changes the way a queryset is paginated to enable non-integer "page numbers".

class List(PaginationMixin, ListView):
paginator_class = KeysetPaginator
paginate_by = 10
queryset = MyModel.objects.order_by('-timestamp', 'group')

You won't be able to iterate through page numbers in a template in the same way: you are limited to next and previous pages. Otherwise, you construct them in largely the same way:

<a href="{% url 'mymodel:list' %}?page={{ page_obj.previous_page_number }}">
Prev Page
</a>

<a href="{% url 'mymodel:list' %}?page={{ page_obj.next_page_number }}">
Next Page
</a>

Note that you do not get access to the length of the queryset, nor the number of pages, because these could be expensive queries. You really don't need to know that ;)

However, I like to use GET forms to [enable pagination of filtered results](https://schinckel.net/2014/08/17/leveraging-html-and-django-forms%3A-pagination-of-filtered-results/):

<button form="target-form"
name="page"
value="{{ page_obj.previous_page_number }}"
type="submit">
&larr; Prev Page
</button>

<button form="target-form"
name="page"
value="{{ page_obj.next_page_number }}"
type="submit">
Next Page &rarr;
<button>


See https://schinckel.net/2018/11/23/keyset-pagination-in-django/ for more details about how this package works.


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-keyset-pagination-plus-0.9.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

django_keyset_pagination_plus-0.9.0-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-keyset-pagination-plus-0.9.0.tar.gz.

File metadata

  • Download URL: django-keyset-pagination-plus-0.9.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.10

File hashes

Hashes for django-keyset-pagination-plus-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4322e35c4c78822acd52bdd191e7c0670a8890436e1f9dc2b5e86ad819c3387c
MD5 4fa4dbf66aa6e6f25ecd9b47a9135df0
BLAKE2b-256 3dcb810cb6108da1d66137866ca22b8c5d81841608e865c7b0033f205aaf759e

See more details on using hashes here.

File details

Details for the file django_keyset_pagination_plus-0.9.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_keyset_pagination_plus-0.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.10

File hashes

Hashes for django_keyset_pagination_plus-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 129f054cc85a85b2268326347ff833848499e53a54fc9c87204000dd1966a170
MD5 63a3ed11159ef35dce34c1e61a8b8285
BLAKE2b-256 e2b5dc8821a743897ecd75e84175eae7f6f88f819be4df2856b07bde8769d8fd

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