Skip to main content

An intelligent compiler tool to convert Django Rest Framework code to Django Ninja.

Project description

🤖 DRF to Django Ninja Compiler

CI License: MIT Python 3.10+

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.

CLI Demo

✨ Features

  • Pydantic Schemas: Parses DRF ModelSerializer and generates standard Ninja ModelSchema.
  • Intelligent Routing: Parses DRF APIView and ModelViewSet and generates clean @router endpoints.
  • Human-in-the-Loop: Automatically flags custom overrides (like SerializerMethodField or custom View methods) and injects helpful TODO comments so you know exactly what needs manual review.
  • Beautiful DX: Powered by Typer and Rich for 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

drf_to_ninja-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

drf_to_ninja-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

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

Hashes for drf_to_ninja-0.1.1.tar.gz
Algorithm Hash digest
SHA256 25d11fa1d801b91eaa1d397ba07560db170b90ac1f9d8ce7969ac1a7eb337553
MD5 d29165fb23ec1eee22eef0329b94a2c3
BLAKE2b-256 024c2f96828df4a0faa6756baac4f76abb2adbbe935507eb12280b3c68e9c5dc

See more details on using hashes here.

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

Hashes for drf_to_ninja-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 55457822e9c506c12b22e22c1a9a07008fa84966b15c423d2fe9450c37501904
MD5 0ab405532322f09ca8fe66866392caf6
BLAKE2b-256 360a0d12411ba0a3069b387ece7401fc6d932b2f7f5324690d70dce196984b41

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