If you're working with Django and want to build fast, clean, and modern APIs, Django Ninja is a powerful tool worth exploring. It brings FastAPI-like speed and type safety, while staying fully integrated with Django.
What is Django Ninja?
Django Ninja is a lightweight web framework for building APIs using Django and Python type hints. It uses Pydantic for data validation and provides automatic Swagger documentation out of the box.
It’s built on top of FastAPI’s ideas, but tailored for Django developers.
Key Features
Django Ninja vs DRF – Quick Comparison
Quick Start
1. Install Django Ninja
CRUD Example: Book API
models.py
Final Thoughts
If you’re already using Django and want a cleaner, faster way to build APIs with full type safety and auto docs, Django Ninja is a solid option. It blends perfectly with the Django ecosystem while offering modern Python practices.
What is Django Ninja?
Django Ninja is a lightweight web framework for building APIs using Django and Python type hints. It uses Pydantic for data validation and provides automatic Swagger documentation out of the box.
It’s built on top of FastAPI’s ideas, but tailored for Django developers.
Key Features
- [] Full support for Python type hints
[] Fast API development with minimal boilerplate
[] Built-in request validation using Pydantic
[] Auto-generated interactive docs (Swagger UI)
[] Supports both sync and async views
[] Easy integration with Django ORM
Django Ninja vs DRF – Quick Comparison
-
[] Speed: Django Ninja is faster out of the box.
[] Type Hints: Full support in Django Ninja, limited in DRF.
[] Learning Curve: Easier with Django Ninja, DRF can be more complex.
[] Async Support: Django Ninja supports async natively.
- Swagger Docs: Built-in with Django Ninja, requires setup in DRF.
Quick Start
1. Install Django Ninja
pip install django-ninja2. Create a basic API
from ninja import NinjaAPI api = NinjaAPI() @api.get("/hello") def hello(request): return {"message": "Hello from Django Ninja!"}3. Add to urls.py
from django.urls import path from .views import api urlpatterns = [ path("api/", api.urls), ]4. Visit Swagger Docs: http://localhost:8000/api/docs
CRUD Example: Book API
models.py
from django.db import models class Book(models.Model): title = models.CharField(max_length=255) author = models.CharField(max_length=255) published_year = models.IntegerField()schemas.py
from ninja import Schema class BookIn(Schema): title: str author: str published_year: int class BookOut(BookIn): id: intviews.py
from ninja import NinjaAPI from .models import Book from .schemas import BookIn, BookOut from django.shortcuts import get_object_or_404 api = NinjaAPI() @api.post("/books", response=BookOut) def create_book(request, data: BookIn): book = Book.objects.create(**data.dict()) return book @api.get("/books", response=list[BookOut]) def list_books(request): return Book.objects.all() @api.get("/books/{book_id}", response=BookOut) def get_book(request, book_id: int): return get_object_or_404(Book, id=book_id) @api.put("/books/{book_id}", response=BookOut) def update_book(request, book_id: int, data: BookIn): book = get_object_or_404(Book, id=book_id) for attr, value in data.dict().items(): setattr(book, attr, value) book.save() return book @api.delete("/books/{book_id}") def delete_book(request, book_id: int): book = get_object_or_404(Book, id=book_id) book.delete() return {"success": True}urls.py
from django.urls import path from .views import api urlpatterns = [ path("api/", api.urls), ]
Final Thoughts
If you’re already using Django and want a cleaner, faster way to build APIs with full type safety and auto docs, Django Ninja is a solid option. It blends perfectly with the Django ecosystem while offering modern Python practices.