Skip to main content

django-toon-rest

Django REST Framework library for rendering responses in TOON format (Token-Oriented Object Notation).

Description

django-toon-rest provides a custom renderer for Django REST Framework that allows returning API responses in TOON format, a lightweight and LLM-friendly format.

Installation

pip install django-toon-rest

This will automatically install required dependencies like json-toon and djangorestframework.

Usage

Basic Configuration

from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer
from django_toon_rest.renderers import TOONRenderer

class ItemViewSet(viewsets.ReadOnlyModelViewSet):
    renderer_classes = [JSONRenderer, TOONRenderer]
    
    def list(self, request, *args, **kwargs):
        serializer = self.get_serializer(self.get_queryset(), many=True)
        return Response(serializer.data)

Content Negotiation

The client must send the Accept: application/toon header to receive the response in TOON format:

curl -H "Accept: application/toon" http://api.example.com/items/

Features

  • ✅ Custom renderer compatible with DRF
  • ✅ Automatic content negotiation
  • ✅ Automatic conversion of Python data to TOON
  • ✅ Compatible with all DRF data structures
  • ✅ Automatic handling of Django Decimal fields

Data Type Handling

The TOONRenderer automatically handles common Django/DRF data types:

  • Decimal fields: Automatically converted to float for JSON serialization
  • Nested objects: Preserved as JSON within table cells (TOON format behavior)
  • Lists and dictionaries: Fully supported with proper formatting

This means you don't need to modify your serializers - the renderer handles type conversions automatically.

Example Project

For a complete working example with models, serializers, and sample data, see the example project:

The example project demonstrates:

  • Models with relationships (ForeignKey, ManyToMany)
  • Nested serializers
  • Multiple endpoints with TOON rendering
  • Sample data fixtures

Dependencies

  • Python >= 3.8
  • Django REST Framework >= 3.0.0 (installed automatically)
  • json-toon >= 1.0.0 (installed automatically)

Development

# Clone the repository
git clone https://github.com/martinartaza/django-toon-rest.git
cd django-toon-rest

# Install in development mode
pip install -e .

Testing

# Optional: dev dependencies
pip install -r requirements-dev.txt

# Run tests
pytest -q

Expected output (example):

4 passed in 0.XXs

License

MIT License

Author

Martin Artaza

Repository

https://github.com/martinartaza/django-toon-rest

Metadata

Release files for django-toon-rest 0.1.3

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-toon-rest 0.1.3
File Size Uploaded
django_toon_rest-0.1.3.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-toon-rest 0.1.3
File Interpreter ABI Platform
django_toon_rest-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / django_toon_rest-0.1.3.tar.gz

Download URL django_toon_rest-0.1.3.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
55670e7d513917630e86e79bcd3a1baae4791116ec96ba727579a3389d605c3d
BLAKE2b-256 checksum
How to use checksums
df21b745865dd80f7473faae32118bb08e680c2b3f60d095ee48d2b20dab1e2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / django_toon_rest-0.1.3-py3-none-any.whl

Download URL django_toon_rest-0.1.3-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48b7fc662ea2893005872f3ee860211315d3fa8e3183e52cfcc6dc229894e2cd
BLAKE2b-256 checksum
How to use checksums
fd02048c84ba9fb1777f13033bd56754530b269c37866827b3c00534f404b1ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

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