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 and Joe Nelson. 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:
<button form="target-form"
name="page"
value="{{ page_obj.previous_page_number }}"
type="submit">
← Prev Page
</button>
<button form="target-form"
name="page"
value="{{ page_obj.next_page_number }}"
type="submit">
Next Page →
<button>
See https://schinckel.net/2018/11/23/keyset-pagination-in-django/ for more details about how this package works.
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-keyset-pagination-plus-0.9.9.tar.gz
.
File metadata
- Download URL: django-keyset-pagination-plus-0.9.9.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c4bc826810750298a20904425fa77f240eeb079106e67ce3e945f38bb06663a |
|
MD5 | 2b51e413de446a405981f9657c78ce70 |
|
BLAKE2b-256 | 029811ce6f03905a3be1343855d090008fa3a38cf6694b2c6d2aa4ff3e793b16 |
File details
Details for the file django_keyset_pagination_plus-0.9.9-py2.py3-none-any.whl
.
File metadata
- Download URL: django_keyset_pagination_plus-0.9.9-py2.py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f0dd5e3db2cbf439a33cea00f76317f5757328c4c8de4897f057bb53b152b3b |
|
MD5 | 86263af12246f4baa682010d85ef9380 |
|
BLAKE2b-256 | 4af6af0d73f28f1586858f24c3224930b6bf731cca11459299d59f1e874e6bfd |