Modern REST framework for Django with types and async support!
- Blazingly fast
- Supports
django>=5.2 - Supports
pydantic2,msgspec,attrs,dataclasses,TypedDictas model schemas, but not bound to any of these libraries - Supports async Django without any
sync_to_asynccalls inside, tested to work with free-threading builds - Fully typed and checked with
mypy,pyright, andpyreflyin strict modes - Supports content negotiation, has default implementations for
json,msgpack, SSE, Json Lines, and more - Strict schema validation of both requests and responses
- Supports
openapi3.1+ schema generation out of the box - Supports all your existing
djangoprimitives and packages, no custom runtimes - Great testing tools with schemathesis, polyfactory, bundled
pytestplugin, and default Django's testing primitives - 100% test coverage with 1700+ of carefully designed unit, integration, and property-based tests
- Built by the community for the community, not a single-person project
- Great docs
- No AI slop, but built for the LLM era
- No emojis 🌚️️
Sync mode
The one thing I really love about django-modern-rest is its pluggable serializers and validators. Frameworks that are tightly coupled with pydantic can be really painful to work with.
— Kirill Podoprigora, CPython core developer
Using
django-modern-resthas been a game-changer for my productivity. The strict type safety and schema validation for both requests and responses mean I spend less time debugging and more time building.
— Josiah Kaviani, Django core developer
Works for:
- CPython 3.11+
- Django 5.2+
pip install django-modern-restThere are several included extras:
'django-modern-rest[msgspec]'providesmsgspecsupport and the fastest json parsing, recommended to be always included'django-modern-rest[pydantic]'providespydanticsupport'django-modern-rest[attrs]'providesattrssupport'django-modern-rest[jwt]'providespyjwtauth support'django-modern-rest[openapi]'providesOpenAPIschema validation and generates better OpenAPI examples withpolyfactory
The shortest example (click here to copy the whole file):
>>> import uuid
>>> import pydantic
>>> from dmr import Body, Controller, Headers
>>> # Or use `dmr.plugins.msgspec` or write your own!
>>> from dmr.plugins.pydantic import PydanticSerializer
>>> class UserCreateModel(pydantic.BaseModel):
... email: str
>>> class UserModel(UserCreateModel):
... uid: uuid.UUID
>>> class HeaderModel(pydantic.BaseModel):
... consumer: str = pydantic.Field(alias='X-API-Consumer')
>>> class UserController(Controller[PydanticSerializer]):
... async def post( # <- can be sync as well!
... self,
... parsed_body: Body[UserCreateModel],
... parsed_headers: Headers[HeaderModel],
... ) -> UserModel:
... """All added props have the correct runtime and static types."""
... assert parsed_headers.consumer == 'my-api'
... return UserModel(uid=uuid.uuid4(), email=parsed_body.email)And then route this controller in your urls.py:
>>> from django.urls import include, path
>>> from dmr.routing import Router
>>> router = Router(
... 'api/',
... [
... path('user/', UserController.as_view(), name='users'),
... ],
... )
>>> urlpatterns = [
... path(router.prefix, include((router.urls, 'my_app'), namespace='api')),
... ]Done! Now you have your shiny API with 100% type safe validation and interactive docs.
Next steps:
- The full documentation has everything you need to get started!
- wemake-django-template can be used to jump-start your new project with
django-modern-rest! - awesome-django-modern-rest - a curated list of resources related to
django-modern-rest!
This project was generated with wemake-python-package. Current template version is: e1fcf312d7f715323dcff0d376a40b7e3b47f9b7. See what is updated since then.