simple-drf-pagination
Zero-boilerplate pagination class factory for Django REST Framework (DRF).
simple-drf-pagination lets you configure page-number, limit/offset, or cursor
pagination inline—without writing a separate pagination subclass for every
view.
Installation
pip install simple-drf-pagination
Requires Python 3.8 or later and Django REST Framework 3.14 or later.
Quick start
Assign the class returned by paginate() to a DRF view or viewset:
from rest_framework.viewsets import ModelViewSet
from simple_drf_pagination import paginate
from .models import Item
from .serializers import ItemSerializer
class ItemViewSet(ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
pagination_class = paginate(page_size=20, max_size=100)
Clients can then request a page and optionally choose its size:
GET /items/?page=2&page_size=50
The default pagination type is page-number pagination, so
paginate(...) and paginate("page", ...) are equivalent.
Pagination types
Page number
pagination_class = paginate("page", page_size=20, max_size=100)
GET /items/?page=3&page_size=50
page_sizesets the default number of results per page.max_sizelimits the client-suppliedpage_sizevalue.
Limit and offset
pagination_class = paginate("limit", page_size=50, max_size=200)
GET /items/?limit=100&offset=200
page_sizebecomes DRF's default limit.max_sizelimits the client-suppliedlimitvalue.
Cursor
pagination_class = paginate(
"cursor",
page_size=25,
ordering="-created_at",
)
The first request does not need a cursor:
GET /items/
Use the opaque cursor URL returned in the response to fetch the next or
previous page. The ordering field should be present on the queryset's model
and should provide a stable ordering. max_size does not apply to cursor
pagination.
Response format
Page-number and limit/offset pagination responses include the total result count, navigation links, and the serialized results:
{
"count": 42,
"next": "http://localhost:8000/items/?page=2",
"previous": null,
"results": [
{
"id": 1,
"name": "Item 1"
}
]
}
Cursor pagination returns navigation links and results, but does not include a total count:
{
"next": "http://localhost:8000/items/?cursor=encoded_cursor",
"previous": null,
"results": [
{
"id": 42,
"name": "Item 42"
}
]
}
The exact URLs and fields inside results depend on the request and serializer.
API reference
paginate(
type: str = "page",
page_size: int = 10,
max_size: int = 100,
ordering: str = "-id",
)
| Parameter | Description |
|---|---|
type |
Pagination strategy: "page", "limit", or "cursor". |
page_size |
Default number of results returned per page. |
max_size |
Maximum client-requested size for page and limit pagination. |
ordering |
Ordering expression used by cursor pagination. |
The function returns a configured DRF pagination class, not an instance.
This is why it can be assigned directly to pagination_class.
Under the hood, each strategy subclasses the corresponding DRF class:
| Type | DRF base class | Configuration |
|---|---|---|
"page" |
PageNumberPagination |
page_size, page_size_query_param="page_size", max_page_size |
"limit" |
LimitOffsetPagination |
default_limit, max_limit |
"cursor" |
CursorPagination |
page_size, ordering |
Passing any other type raises ValueError:
paginate("unknown") # ValueError: Invalid pagination type
License
Released under the MIT License.
Metadata
Release files for simple-drf-pagination 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simple_drf_pagination-0.1.3.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_drf_pagination-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.8 kB
Release files / simple_drf_pagination-0.1.3.tar.gz
| Download URL | simple_drf_pagination-0.1.3.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34c944e4e403d2c0b859bdcbde5c50ac169a0edf94995d699a2803e658e480d8
|
|
BLAKE2b-256 checksum How to use checksums |
8e32095511986d438c5ef915726cacb01c1a5963682f2ed79f7e4d6010dc9824
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.0
|
Release files / simple_drf_pagination-0.1.3-py3-none-any.whl
| Download URL | simple_drf_pagination-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1fb4122162b86e00a3e9c826e88f95c0a1b32b30ac87eef402cb97ca24dc164
|
|
BLAKE2b-256 checksum How to use checksums |
47c9d262abfe518d69d38ba3c46ab174df6329879e9a1a753939b0f7a5ac477a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.0
|