An intelligent compiler tool to convert Django Rest Framework code to Django Ninja.
Project description
🤖 DRF to Django Ninja Compiler
An intelligent, user-friendly compiler tool designed to automatically parse Django Rest Framework (DRF) code (serializers.py, views.py) and convert it into modern, fast Django Ninja equivalents (schemas.py, api.py).
Tired of manually migrating hundreds of legacy DRF endpoints? This tool automates the heavy lifting by leveraging Abstract Syntax Tree (AST) parsing to reverse-engineer your code.
✨ Features
- Pydantic Schemas: Parses DRF
ModelSerializerand generates standard NinjaModelSchema. - Intelligent Routing: Parses DRF
APIViewandModelViewSetand generates clean@routerendpoints. - Human-in-the-Loop: Automatically flags custom overrides (like
SerializerMethodFieldor custom View methods) and injects helpfulTODOcomments so you know exactly what needs manual review. - Beautiful DX: Powered by
TyperandRichfor a stunning terminal experience.
� Before & After
Your legacy DRF code:
# serializers.py
class OrderSerializer(serializers.ModelSerializer):
total = serializers.SerializerMethodField()
class Meta:
model = Order
fields = ['id', 'status', 'total']
def get_total(self, obj):
return sum(item.price for item in obj.items.all())
What the compiler generates:
# schemas.py (auto-generated)
class OrderSchema(ModelSchema):
# ⚠️ USER REVIEW REQUIRED:
# The compiler detected custom fields or methods in the DRF Serializer:
# - total
# - method:get_total
# You will need to manually map these to Pydantic types or Resolve() blocks.
class Meta:
model = Order
fields = ['id', 'status', 'total']
Your legacy DRF ViewSet:
# views.py
class OrderViewSet(viewsets.ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrderSerializer
def list(self, request): ...
def create(self, request): ...
What the compiler generates:
# api.py (auto-generated)
@router.get('/order/', response=list[OrderSchema])
def list_order(request):
"""Automatically generated list view for OrderViewSet."""
return Order.objects.all()
@router.post('/order/', response=OrderSchema)
def create_order(request, payload: OrderInSchema):
"""Automatically generated create view for OrderViewSet."""
# TODO: Implement creation logic using payload.dict()
pass
�🚀 Installation
git clone https://github.com/lektronik/drf-to-ninja-compiler.git
cd drf-to-ninja-compiler
python3 -m venv venv
source venv/bin/activate
pip install -e .
🛠 Usage
Simply point the compiler at your existing DRF files:
# Compile both serializers and views simultaneously
drf2ninja --serializers myapp/serializers.py --views myapp/views.py
# Or just compile serializers
drf2ninja -s myapp/serializers.py
🔒 Security & Code Quality
This project uses pre-commit hooks to ensure code quality and security.
- Formatting:
black - Security Analysis:
bandit
To set up the hooks locally:
pip install pre-commit
pre-commit install
🤝 Contributing
Pull requests are welcome. Make sure your changes pass the automated test suite:
pytest tests/
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_to_ninja-0.1.1.tar.gz.
File metadata
- Download URL: drf_to_ninja-0.1.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25d11fa1d801b91eaa1d397ba07560db170b90ac1f9d8ce7969ac1a7eb337553
|
|
| MD5 |
d29165fb23ec1eee22eef0329b94a2c3
|
|
| BLAKE2b-256 |
024c2f96828df4a0faa6756baac4f76abb2adbbe935507eb12280b3c68e9c5dc
|
File details
Details for the file drf_to_ninja-0.1.1-py3-none-any.whl.
File metadata
- Download URL: drf_to_ninja-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55457822e9c506c12b22e22c1a9a07008fa84966b15c423d2fe9450c37501904
|
|
| MD5 |
0ab405532322f09ca8fe66866392caf6
|
|
| BLAKE2b-256 |
360a0d12411ba0a3069b387ece7401fc6d932b2f7f5324690d70dce196984b41
|