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
floatfor 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:
- Repository: django-toon-rest-example (coming soon)
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
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)
| File | Size | Uploaded | |
|---|---|---|---|
| django_toon_rest-0.1.3.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|