Django Modern REST: A High-Performance API Framework for Django
Django Modern REST (DMR) is a next-generation REST API framework built for Django—outperforming both Django REST Framework (DRF) and django-ninja in speed and flexibility. It achieves performance within 30% of FastAPI (excluding database queries). DMR supports Pydantic v2, msgspec, attrs, dataclasses, TypedDict, and NamedTuple as serialization models. It delivers full type safety, OpenAPI 3.1/3.2 compliance, and seamless sync/async support.
Key Features
DMR maintains full compatibility with the Django ecosystem: Controllers inherit from Django’s View, and standard Django plugins work out of the box. It includes content negotiation for JSON/msgpack, streaming via SSE and JSON Lines, and strict response schema validation in development mode.
- Speed: Benchmarks confirm DMR leads all Django-based frameworks.
- Serializer Flexibility: Choose serializers at the controller level—and optionally integrate DRF serializers.
- Type Safety: Full, uncompromised typing for both request payloads and responses.
- OpenAPI: Automatic, state-of-the-art OpenAPI 3.1/3.2 schema generation.
- Testing: Native integration with schemathesis, polyfactory, and a pytest plugin for property-based testing.
Class-based controllers simplify code reuse and plugin extensibility.
Minimal Working Application
A fully functional API in just 71 lines:
import secrets
import sys
import uuid
import pydantic
from django.conf import settings
from django.core.management import execute_from_command_line
from django.urls import include
from dmr import Body, Controller
from dmr.openapi import build_schema
from dmr.openapi.views import OpenAPIJsonView, SwaggerView
from dmr.plugins.pydantic import PydanticSerializer
from dmr.routing import Router, path
if not settings.configured:
settings.configure(
ROOT_URLCONF=__name__,
ALLOWED_HOSTS='*',
DEBUG=True,
INSTALLED_APPS=['dmr', 'django.contrib.staticfiles'],
STATIC_URL='/static/',
STATICFILES_FINDERS=[
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
],
TEMPLATES=[
{
'APP_DIRS': True,
'BACKEND': 'django.template.backends.django.DjangoTemplates',
},
],
SECRET_KEY=secrets.token_hex(),
)
class UserCreateModel(pydantic.BaseModel):
email: str
class UserResponseModel(UserCreateModel):
uid: uuid.UUID
class UserController(Controller[PydanticSerializer]):
async def post(
self,
parsed_body: Body[UserCreateModel],
) -> UserResponseModel:
return UserResponseModel(uid=uuid.uuid4(), email=parsed_body.email)
router = Router(
'api/',
[
path('user/', UserController.as_view(), name='users'),
],
)
schema = build_schema(router)
urlpatterns = [
path(router.prefix, include((router.urls, 'your_app'), namespace='api')),
path('docs/openapi.json/', OpenAPIJsonView.as_view(schema), name='openapi'),
path('docs/swagger/', SwaggerView.as_view(schema), name='swagger'),
]
if __name__ == '__main__':
execute_from_command_line(sys.argv)
Run with python file.py runserver. Swagger docs are available at /docs/swagger/.
Comparison With Competitors
DRF suffers from a single serializer for both requests and responses, accidental model field leakage, and weak type support. django-ninja relies on function-based routing, enforces Pydantic exclusively, and offers limited OpenAPI customization.
| Feature | DRF | django-ninja | DMR |
|---------|-----|--------------|-----|
| Type Safety | None | Partial | Full |
| Speed | Low | Medium | High |
| OpenAPI Support | Via third-party plugins | Limited | Native OpenAPI 3.1/3.2 |
| Flexibility | Low | Medium | High |
DMR addresses architectural pain points in Django by embracing modern patterns—like those used in the [wemake-django-template](https://github.com/wemake-services/wemake-django-template).
Modern Python & Threading Support
With free-threading coming to CPython, asyncio’s dominance is fading. As core Python developers observe:
- Guido van Rossum: "asyncio in stdlib has frozen ecosystem innovation."
- Mark Shannon: "Virtual threads deliver better ergonomics than async/await."
- Armin Ronacher: "In languages with real threading, threads consistently outperform async."
Unlike FastAPI, Django (and DMR) supports both sync and async paradigms without lock-in.
Developer Tooling
- Testing: schemathesis enables property-based API testing covering ~90% of endpoints; polyfactory auto-generates realistic test data.
- AI Integration: Includes ready-to-use LLM prompts (
llms-full.txt), Context7, DeepWiki, and migration-focused templates. - Validation: Strict schema validation in development; configurable per-environment in production.
The framework is fully documented, rigorously typed, and contains zero AI-generated code.
Why It Matters
- Near-FastAPI performance—with full Django compatibility.
- Multiple serialization backends—no vendor lock-in to one library.
- Class-based controllers for clean reuse and plugin ecosystems.
- First-class OpenAPI 3.1/3.2 support and strict dev-time schema validation.
- Built-in tooling for robust, property-based API testing.
— Editorial Team
No comments yet.