Skip to main content

A Django app to help make building APIs great.

Project description

Django Dans API Toolkit

Lint Test Types 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']

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
Copyright 2025 © Daniel Nazarian.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-dans-api-toolkit-1.1.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dans_api_toolkit-1.1.1-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file django-dans-api-toolkit-1.1.1.tar.gz.

File metadata

  • Download URL: django-dans-api-toolkit-1.1.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for django-dans-api-toolkit-1.1.1.tar.gz
Algorithm Hash digest
SHA256 76f63cff07c48654762732ce47cd7cb2f1d935b872e9c9d3a605234f7bbda9b3
MD5 0bc38c18f9ccf5ec909f90acc0fb5c96
BLAKE2b-256 3cbcd55f767af32805a3a40a7f2acc51912e921ae4374ddcbf07300319b1f8ca

See more details on using hashes here.

File details

Details for the file django_dans_api_toolkit-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dans_api_toolkit-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b95ced7fbaea66b86c0c0f68f754d11cde9e76b5423724a94406e391e0c21910
MD5 0f5947b569ed2e2f4bddbeff8c13e91b
BLAKE2b-256 10ad9bb5f5f38c86e867c443bb18f2a5b346f07b84114240f56f27a78d13f2d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page