Custom pagination classes for Django REST Framework
Project description
Django Easy Pagination
A Django REST Framework library providing customizable pagination classes with rich metadata and flexible configuration options.
Features
Rich Metadata: Get comprehensive pagination information including total pages, current page, and page size
Multiple Pagination Classes: Pre-configured classes for different use cases
Highly Customizable: Easy to customize page sizes and behavior
Client Control: Allow clients to specify page size via query parameters
Production Ready: Includes comprehensive test suite
Well Documented: Clear documentation and examples
Installation
Install using pip:
pip install django-easy-pagination
Or install from source:
git clone https://github.com/casperspec-1/easy-pagination.git
cd easy-pagination
pip install -e .
Quick Start
1. Add to your Django REST Framework view:
from rest_framework import viewsets
from easy_pagination import StandardPagination
class MyViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MySerializer
pagination_class = StandardPagination
2. Get paginated responses with rich metadata:
{
"count": 100,
"next": "http://api.example.com/items/?page=3",
"previous": "http://api.example.com/items/?page=1",
"total_pages": 10,
"current_page": 2,
"page_size": 10,
"results": [...]
}
Available Pagination Classes
StandardPagination
Default pagination for most list views.
- Default page size: 20 items
- Max page size: 100 items
- Best for: General purpose API endpoints
from easy_pagination import StandardPagination
class MyViewSet(viewsets.ModelViewSet):
pagination_class = StandardPagination
SmallResultsPagination
Optimized for small, quick-loading lists.
- Default page size: 10 items
- Max page size: 50 items
- Best for: Dropdown lists, autocomplete, quick searches
from easy_pagination import SmallResultsPagination
class QuickListViewSet(viewsets.ModelViewSet):
pagination_class = SmallResultsPagination
LargeResultsPagination
Designed for large datasets and reports.
- Default page size: 50 items
- Max page size: 500 items
- Best for: Reports, data exports, admin interfaces
from easy_pagination import LargeResultsPagination
class ReportViewSet(viewsets.ModelViewSet):
pagination_class = LargeResultsPagination
NoPagination
Disable pagination for specific views.
- Returns: All results without pagination
- Best for: Small datasets, configuration endpoints
- Warning: Use carefully with large datasets!
from easy_pagination import NoPagination
class ConfigViewSet(viewsets.ModelViewSet):
pagination_class = NoPagination
Advanced Usage
Custom Pagination Class
Create your own pagination class by extending CustomPageNumberPagination:
from easy_pagination import CustomPageNumberPagination
class MyCustomPagination(CustomPageNumberPagination):
page_size = 25
max_page_size = 200
page_size_query_param = 'page_size'
Dynamic Pagination
Use the factory function to create pagination classes on the fly:
from easy_pagination import get_pagination_class
class MyViewSet(viewsets.ModelViewSet):
pagination_class = get_pagination_class(page_size=30, max_page_size=200)
Client-Controlled Page Size
Clients can control page size using query parameters:
# Get 50 items per page
GET /api/items/?page_size=50
# Navigate to page 3
GET /api/items/?page=3
# Combine both
GET /api/items/?page=3&page_size=50
Global Configuration
Set a default pagination class for all views in your Django settings:
# settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'easy_pagination.StandardPagination',
'PAGE_SIZE': 20
}
Response Schema
All pagination classes return responses with the following structure:
| Field | Type | Description |
|---|---|---|
count |
integer | Total number of items across all pages |
next |
string/null | URL to the next page (null if on last page) |
previous |
string/null | URL to the previous page (null if on first page) |
total_pages |
integer | Total number of pages |
current_page |
integer | Current page number (1-indexed) |
page_size |
integer | Number of items per page |
results |
array | Array of serialized objects for current page |
Requirements
- Python >= 3.7
- Django >= 3.2
- djangorestframework >= 3.12
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/casperspec-1/easy-pagination.git
cd easy-pagination
# Install in development mode
pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=easy_pagination
# Run specific test file
pytest tests/test_pagination.py
Code Quality
# Format code with black
black easy_pagination/
# Sort imports
isort easy_pagination/
# Lint with flake8
flake8 easy_pagination/
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Casper
- Email: cassymyo@gmail.com
- GitHub: @casperspec-1
Changelog
See CHANGELOG.md for a list of changes.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
Acknowledgments
Built with ❤️ using Django REST Framework.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easy_pagination-0.1.0.tar.gz.
File metadata
- Download URL: easy_pagination-0.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fab772777e1811ccc3a13ae2fad236e3dfafd747b801b186469fcff3c1506591
|
|
| MD5 |
9d77a318df7a6a30af73d55eb1bbc34a
|
|
| BLAKE2b-256 |
9a6eb9ec44b9e76b469fc71aca3c0f928b06d5910fef14d3fa3cc5e7bce6f6e5
|
Provenance
The following attestation bundles were made for easy_pagination-0.1.0.tar.gz:
Publisher:
publish.yml on DigitalTouchCode/easy-pagination
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
easy_pagination-0.1.0.tar.gz -
Subject digest:
fab772777e1811ccc3a13ae2fad236e3dfafd747b801b186469fcff3c1506591 - Sigstore transparency entry: 775106552
- Sigstore integration time:
-
Permalink:
DigitalTouchCode/easy-pagination@8f60b32b17823cac2cc985540b875f87f18fd87b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/DigitalTouchCode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f60b32b17823cac2cc985540b875f87f18fd87b -
Trigger Event:
release
-
Statement type:
File details
Details for the file easy_pagination-0.1.0-py3-none-any.whl.
File metadata
- Download URL: easy_pagination-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5ac4633aaf3e423e08832864a392948338f6650d3105f1eb876a236029e5e7a
|
|
| MD5 |
efb9d94ecdaf9b7c2ab7e8f0e8c3e5c4
|
|
| BLAKE2b-256 |
9dded0b4f9b2274071d4a17d04d3e3aa949540a272eb2f1da9d8872b70dd4ded
|
Provenance
The following attestation bundles were made for easy_pagination-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on DigitalTouchCode/easy-pagination
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
easy_pagination-0.1.0-py3-none-any.whl -
Subject digest:
f5ac4633aaf3e423e08832864a392948338f6650d3105f1eb876a236029e5e7a - Sigstore transparency entry: 775106553
- Sigstore integration time:
-
Permalink:
DigitalTouchCode/easy-pagination@8f60b32b17823cac2cc985540b875f87f18fd87b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/DigitalTouchCode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f60b32b17823cac2cc985540b875f87f18fd87b -
Trigger Event:
release
-
Statement type: