Skip to main content

Django Dans API Toolkit

Lint Test Types TestPyPI codecov

Python Versions Django Versions PyPI Version Downloads License Codacy grade Code style

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.

Available on PyPi

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

  1. Install the package via pip:

    pip install django-dans-api-toolkit
    
  2. Add django_dans_api_toolkit to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        ...
        'django_dans_api_toolkit',
    ]
    
  3. Run migrations to update your database schema:

    python manage.py migrate
    
  4. 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_CLASSES and DEFAULT_PAGINATION_CLASS in your settings.py to work with the APIs properly. An example config would be:

https://danielnazarian.com

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)

Source distribution for django-dans-api-toolkit 1.3.0
File Size Uploaded
django_dans_api_toolkit-1.3.0.tar.gz 23.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-dans-api-toolkit 1.3.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page