A small package for easy use list apis in django rest framework, I call this ALP.
Project description
api-list-pagination
🎉 A powerful and flexible pagination utility for Django REST Framework (DRF) that simplifies pagination, listing, and exporting data to Excel with ease. Say goodbye to repetitive pagination logic! 🚀
Features ✨
- 🧙 Flexible Pagination: Automatically handle paginated responses with customizable page sizes.
- 📜 Listing Mode: Retrieve all data without pagination, optionally filtering fields.
- 📊 Excel Export: Export data directly to Excel-like responses.
- 🔍 Distinct Query: Easily return distinct results for your queries.
Installation 🛠️
-
Install the package from PyPI (coming soon):
pip install api-list-pagination
-
Import and use it in your Django REST Framework views.
Quick Start 🚀
1. Paginate Your Data
from api_list_pagination import ALP
from rest_framework.views import APIView
class ExampleView(APIView):
def get(self, request):
queryset = MyModel.objects.all()
serializer = MySerializer
paginator = ALP(request, queryset, serializer)
return paginator.ret()
2. Retrieve a Full Listing (No Pagination)
class FullListView(APIView):
def get(self, request):
queryset = MyModel.objects.all()
serializer = MySerializer
paginator = ALP(request, queryset, serializer)
response = paginator.listing()
return response
Example Request:
GET /example/?limit=-1&fields=name age
3. Export Data to Excel
class ExcelExportView(APIView):
def get(self, request):
queryset = MyModel.objects.all()
serializer = MySerializer
paginator = ALP(request, queryset, serializer)
response = paginator.excel()
return response
Example Request:
GET /example/?excel=1&fields=name email
How It Works ⚙️
ALP simplifies common data retrieval scenarios with:
pagination(): Paginated results using DRF'sPageNumberPagination.listing(): Non-paginated results with optional field filtering.excel(): Converts data into Excel-like responses.
It uses the limit query parameter to control pagination and supports additional options like:
distinct: Ensure unique results.fields: Retrieve specific fields from the queryset.values_list: Output data in list format for single or multiple fields.
API Reference 📖
ALP(request, data, serializer, context=None)
| Parameter | Type | Description |
|---|---|---|
request |
HttpRequest |
The DRF request object. |
data |
QuerySet |
The Django queryset to paginate. |
serializer |
Serializer |
The DRF serializer class to use. |
context |
dict (optional) |
Additional serializer context (default: {}). |
Methods:
ret(): Automatically decide between pagination, listing, or Excel export.pagination(): Perform pagination on the queryset.listing(): Retrieve all data without pagination.excel(): Export data to Excel-like format.
Examples 📚
Example 1: Paginated Results
Request:
GET /example/?limit=5
Response:
{
"count": 100,
"next": "http://api.example.com/example/?limit=5&offset=5",
"previous": null,
"results": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
{"id": 3, "name": "Charlie"},
{"id": 4, "name": "Diana"},
{"id": 5, "name": "Eve"}
]
}
Example 2: Distinct Results
Request:
GET /example/?distinct=1&fields=name
Response:
{
"results": [
{"name": "Alice"},
{"name": "Bob"},
{"name": "Charlie"}
]
}
Example 3: Excel Export
Request:
GET /example/?excel=1&fields=name email
Response:
[
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob", "email": "bob@example.com"}
]
Contributing 🛠️
We welcome contributions! Feel free to fork the repository, submit issues, or create pull requests. Let’s build something awesome together. 💪
License 📄
This project is licensed under the MIT License.
Support 💬
Have questions or feedback? Feel free to reach out or open an issue on GitHub. We're here to help!
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 api_list_pagination-0.0.2.tar.gz.
File metadata
- Download URL: api_list_pagination-0.0.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ced0634e0a5f77486ad483ca896c581436ee7b462ce41f9815c356f5004e641
|
|
| MD5 |
e17d326c080cd6dceac43e2dcb8167a9
|
|
| BLAKE2b-256 |
fee97e307b3030008d3b6fb4a98e013d215a1d83f76d32374f9cf96a8c646512
|
File details
Details for the file api_list_pagination-0.0.2-py3-none-any.whl.
File metadata
- Download URL: api_list_pagination-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66a0c82e94f1fa99e6228fc9ea0c65a9b2a38969e8784b983f507de8c70811f7
|
|
| MD5 |
ed90405a0ea4b53ddb2fc8c6f23ba436
|
|
| BLAKE2b-256 |
761db5d6247d4b0aff0cb55fe0c717137f9b6eeaa347fe5eb4cce2c4c6a5b216
|