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.

✨ 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):
    # ⚠️ HUMAN INTERVENTION 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.0.tar.gz (11.0 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.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file drf_to_ninja-0.1.0.tar.gz.

File metadata

  • Download URL: drf_to_ninja-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 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.0.tar.gz
Algorithm Hash digest
SHA256 e64f0d70019651ce096ce2bb85d16a7e83e749467b38c8f701db289ba07f6e94
MD5 208d64a44abf0e80d330d6aed81a01b2
BLAKE2b-256 9d6c712526f96c61404d222ff32964c4967023b71f470c0071706ac56124b512

See more details on using hashes here.

File details

Details for the file drf_to_ninja-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: drf_to_ninja-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3c66f0811632e7f32a3b56744f4ddc449e119343e8e684666988e2733e6dfbe
MD5 d9ceb242aff15a4adfbb8e79b7bbede8
BLAKE2b-256 f1204f3e143ecb3c4e3ef0947df2347907dd231871df7cddf509f656b4a1e0f5

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