Registry / web-framework / django-ninja

django-ninja

JSON →
library1.6.3pypypi✓ verified 24d ago

Django Ninja is a fast, modern web framework for building APIs with Django using Python type hints, Pydantic, and automatic OpenAPI documentation. It aims for high performance and developer friendliness, similar to FastAPI, while leveraging Django's ecosystem. The library maintains a regular release cadence with frequent updates and bug fixes.

pip install django-ninja
INSTALL
IMPORT
SIG · DJANGO-NINJA
D
django-ninja
web-frameworkpythonv1.6.3
Install
5.3s avg
Import
Disk
84MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 84.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.3s · import 0.000s · 84MB
84MB installed
● package 84MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

NinjaAPI
from ninja import NinjaAPI
Router
from ninja import Router
ModelSchema
from ninja import ModelSchema
class MySchema(ModelSchema): class Config: # Deprecated in Pydantic V2, use ModelSchema.Meta in Django Ninja ...
For `ModelSchema`, `django-ninja` introduced `Meta` for consistency with Django's `ModelForms`. While `Config` is still supported, `Meta` is preferred and more aligned with Pydantic V2's `model_config` concept for custom configurations.
Query
from ninja import Query
Path
from ninja import Path
Body
from ninja import Body
File
from ninja import File
UploadedFile
from ninja.files import UploadedFile

This quickstart demonstrates creating a basic API endpoint. Define a `NinjaAPI` instance, then use decorator functions (e.g., `@api.get`) to define API routes. Parameters with type hints are automatically validated and converted. The API instance's `.urls` should be included in your Django project's `urlpatterns`.

from django.contrib import admin from django.urls import path from ninja import NinjaAPI # In your Django project's main urls.py: # from .api import api # Assuming api.py is next to urls.py # urlpatterns = [ # path('admin/', admin.site.urls), # path('api/', api.urls) # Mount the NinjaAPI # ] # Create an api.py file in your Django project's root directory (next to urls.py) # api.py api = NinjaAPI(title="My Awesome API", version="1.0.0") @api.get("/hello") def hello(request): return "Hello from Django Ninja!" @api.get("/add") def add(request, a: int, b: int): return {"result": a + b}
null --version
Debug
Known issues
breakingDjango Ninja v1.0 and later versions require Pydantic V2. Projects upgrading from older Django Ninja versions (0.x) or using Pydantic V1 will encounter breaking changes due to Pydantic's major rewrite.
fix
Upgrade Pydantic to V2 and refactor Pydantic models according to Pydantic V2 migration guides. Django Ninja's `ModelSchema.Meta` class replaces `BaseModel.Config` for model-based schemas.
affects: >=1.0.0
gotchaDirectly calling Django ORM methods (e.g., `MyModel.objects.all()`) within `async` API views will block the event loop and raise an error. The Django ORM is not fully async-native yet.
fix
Wrap all blocking (synchronous) ORM calls and other sync I/O operations with `sync_to_async` from `asgiref.sync`. For example: `queryset = await sync_to_async(MyModel.objects.all)()` or `obj = await sync_to_async(MyModel.objects.get)(pk=id)`.
affects: All
deprecatedThe previous method of returning a tuple `(status_code, body)` to specify HTTP status codes in responses is deprecated.
fix
Use the `Status` class explicitly for returning responses with custom HTTP status codes. For example, `return Status(200, {'message': 'Success'})`.
affects: >=1.6.0
breakingAs of v1.6.0, routers became idempotent and reusable. While this is an enhancement, it changes behavior around mounting the same router multiple times. Previous workarounds or reliance on non-idempotent behavior might need review.
fix
Review any code that mounts the same `Router` instance multiple times to ensure the new idempotent behavior (where decorators, auth, tags, and throttling are fully isolated between mounts) aligns with expectations. This generally resolves previous errors for such patterns.
affects: >=1.6.0
gotchaStandard Django decorators (e.g., `@cache_page`, `@transaction.atomic`) do not directly integrate with Django Ninja operation functions without explicit wrapping.
fix
For function-based operations, you might need to manually wrap the Django decorator around the `django-ninja` decorator, or use `router.add_decorator` with a custom universal decorator that handles both sync/async. The `decorate_view` utility or custom middleware might also be necessary for certain scenarios. Refer to the 'Decorators' section of the official documentation.
affects: All
Upgrade
Version history
1.6.3latest on PyPI · released Aug 17, 2026
Audit
Dependencies
DjangorequiredCore web framework integration.
PydanticrequiredData validation, serialization, and settings management via type hints.
Agent activity
26 hits · last 30 days
node
22
Resources
django-ninja — pip install django-ninja · libregistry