Django Ninja
Episode #490,
published Tue, Dec 24, 2024, recorded Tue, Dec 10, 2024
If you're a Django developer, I'm sure you've heard so many people raving about FastAPI and Pydantic. But you really love Django and don't want to switch. Then you might want to give Django Ninja a serious look. Django Ninja is highly inspired by FastAPI, but is also deeply integrated into Django itself. We have Vitaliy Kucheryaviy the creator of Django Ninja on this show to tell us all about it.
Links from the show
Vitaly: github.com/vitalik
Vitaly on X: @vital1k
Top 5 Episodes of 2024: talkpython.fm/blog/posts/top-talk-python-podcast-episodes-of-2024
Django Ninja: django-ninja.dev
Motivation section we talked through: django-ninja.dev/motivation
LLM for Django Ninja: llm.django-ninja.dev
Nano Django: github.com/vitalik/nano-django
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy
Vitaly on X: @vital1k
Top 5 Episodes of 2024: talkpython.fm/blog/posts/top-talk-python-podcast-episodes-of-2024
Django Ninja: django-ninja.dev
Motivation section we talked through: django-ninja.dev/motivation
LLM for Django Ninja: llm.django-ninja.dev
Nano Django: github.com/vitalik/nano-django
Episode transcripts: talkpython.fm
--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy
Episode Breakdown and Key Takeaways
1. What is Django Ninja?
- Inspired by FastAPI but integrates deeply with Django.
- Created by Vitaly Kucheryaviy, who needed a FastAPI-like approach without leaving Django.
- Uses type hints and Pydantic to handle validation and serialization automatically.
2. Why Another Tool Alongside the Django REST Framework?
- Django Ninja is lighter-weight and highly focused on minimal boilerplate with modern Python typing.
- Django REST Framework is more established, but can feel more heavyweight.
- Django Ninja aims to keep code succinct, letting developers rely on type annotations to validate data from queries, JSON, forms, etc.
3. Automatic Documentation
- Much like FastAPI, Django Ninja auto-generates interactive API docs (Swagger or Redoc) from type hints and schemas.
- Docs can be restricted or secured if you don’t want them publicly available.
4. Async Support
- Django Ninja supports async view functions for significant performance gains, especially in I/O-bound work.
- Integrates with Django’s evolving async capabilities (such as async ORM methods).
- You can freely mix sync and async views in the same project.
5. Versioning and Multiple API Instances
- Easily define multiple API “instances” (e.g.,
/api/v1/
,/api/v2/
) within a single Django project. - This enables side-by-side deployment of different API versions or separate public vs. private endpoints.
6. Handling Inputs (JSON, Forms, Headers, etc.)
- Simple to parse data from query parameters, headers, path parameters, forms, or JSON—just annotate function parameters.
- Leverages Pydantic-like schemas for complex data structures.
7. Authentication
- Uses a flexible “auth” hook: attach your own logic for API keys, tokens, or Django’s built-in session auth.
- Not strictly tied to user objects—whatever object or data you return from the auth function is attached to the request.
8. Day-to-Day Use and Performance
- Many teams see reduced server load switching from heavier frameworks.
- The design philosophy: “lazy” developer ergonomics—less boilerplate, more features from typing.
9. Nano Django
- A related mini-project that bundles Django + Django Ninja in a single file for quick experiments.
- Can expand your one-file app into a full Django project structure once it grows.
10. Large Language Models (LLMs) and Docs
- The conversation noted that LLMs often serve older syntax. Always check the official Django Ninja docs for the latest details.
- There’s also a future plan for LLM.djangoNinja.dev to redirect to an issue tracking LLM-related documentation support.
Overall Takeaway If you love Django but want a modern, FastAPI-style experience—with strong typing, auto-generated docs, minimal boilerplate, and optional async—Django Ninja is worth exploring. It retains the familiar Django feel (ORM, request/user handling) while adopting newer Python features for cleaner API development.