Django Dans API Toolkit
Description
Django Dans API Toolkit is a Django app to help make building APIs great.
It provides a collection of tools for common API tasks, intended to complement Django Rest Framework (DRF) rather than replace it.
Features
The toolkit includes:
- API Response Handler (
api_response_handler.py): Standardizes API response formats with enhanced error logging that automatically captures stack traces for Exception objects. Now with robust error message extraction that supports arbitrarily nested error structures (for example, DRF ValidationError details like{ 'user': { 'profile': { 'email': ['Invalid email.'] } }, 'password': ['Too short.'] }). The handler will always surface the first relevant string error it finds, even if it is several levels deep in a dict or list, ensuring user-friendly error messages regardless of how complex the error structure is. Type annotations and docstrings have also been improved for clarity and best practices. - API Response Renderer (
api_response_renderer.py): Ensures consistent response rendering across your APIs. - Base Serializer (
serializers/base.py): Simplifies serializer creation with masking and reference functionality.
Quick Start
Installation
-
Install the package via pip:
pip install django-dans-api-toolkit
-
Add
django_dans_api_toolkitto yourINSTALLED_APPSsetting:INSTALLED_APPS = [ ... 'django_dans_api_toolkit', ]
-
Run migrations to update your database schema:
python manage.py migrate
-
Use the provided tools in your views and serializers.
Example Usage
API Response Handler
from django_dans_api_toolkit.api_response_handler import ApiResponseHandler
from django.core.exceptions import ValidationError
def my_view(request):
handler = ApiResponseHandler()
try:
# Your API logic here
serializer.is_valid(raise_exception=True)
data = {"key": "value"}
return handler.response_success(results=data)
except ValidationError as e:
# Exception objects automatically get full stack traces in logs!
# Error message extraction also supports arbitrarily nested error structures.
return handler.response_error(
message="Validation failed",
error=e,
error_fields=serializer.errors
)
Base Serializer
from django_dans_api_toolkit.serializers.base import BaseSerializer
from myapp.models import MyModel
class MyModelSerializer(BaseSerializer):
class Meta:
model = MyModel
fields = '__all__'
ref_fields = ['field1', 'field2']
masked_fields = ['field3'] # removed unless masked=False; mask_as_null=True keeps it as null
API Response Renderer
Ensure DEFAULT_AUTHENTICATION_CLASSES and DEFAULT_PAGINATION_CLASS are set in your settings.py for proper API functionality. Set the renderer itself as well.
Example configuration:
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 20,
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.TokenAuthentication",
),
}
Requirements
- Python 3.10 - 3.11
- Django 3.1 or higher
- Django Rest Framework
- NOTE: not only must you have this installed, you must have set
DEFAULT_AUTHENTICATION_CLASSESandDEFAULT_PAGINATION_CLASSin yoursettings.pyto work with the APIs properly. An example config would be:
- NOTE: not only must you have this installed, you must have set
https://danielnazarian.com
Copyright 2025 © Daniel Nazarian.
Release files for django-dans-api-toolkit 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_dans_api_toolkit-1.3.0.tar.gz | 23.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_dans_api_toolkit-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 46.2 kB
Release files / django_dans_api_toolkit-1.3.0.tar.gz
| Download URL | django_dans_api_toolkit-1.3.0.tar.gz |
|---|---|
| Size | 23.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7c8e51dc9ffb38f87c638c4ffe3474a45c42664450f895eb7f845e16a65459b
|
|
BLAKE2b-256 checksum How to use checksums |
0423c637b091f6f7414b169b41ef53d2b212799813e615afbd5fce39c2b1a059
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / django_dans_api_toolkit-1.3.0-py3-none-any.whl
| Download URL | django_dans_api_toolkit-1.3.0-py3-none-any.whl |
|---|---|
| Size | 23.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71ea7c0420a6aa24067812b069466baaf96b57fade60b0be15e697f0e2cf72cb
|
|
BLAKE2b-256 checksum How to use checksums |
cb6afde71825c3417bc0779de6e69ad7f275b3499c112ef62a25b82c7764c086
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|