Skip to main content

Django REST Framework renderer for TOON format

Project description

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

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_toon_rest-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

django_toon_rest-0.1.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django_toon_rest-0.1.2.tar.gz.

File metadata

  • Download URL: django_toon_rest-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for django_toon_rest-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6984339be37e377988aea432dd54b43ea1c2f0dc08b85f4b8037d91eb8845e46
MD5 85895509faf0f7842221b6a44e4f8420
BLAKE2b-256 906c791c4e764f237201dabb2837980768b3e3172e074278e4806245913e8b82

See more details on using hashes here.

File details

Details for the file django_toon_rest-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_toon_rest-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 61aed29388adf55168607997b07f669232b76e0e7e06ed5a8e34f8a60e64a9e3
MD5 1a29419068b125b37c451c384a6d7c3a
BLAKE2b-256 c3e5b9306571234280455f11d2c74bd26b5041e4b3050ad6d28b391ffbd5255e

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