wemake-services/django-modern-rest

Modern REST framework for Django with types and async support!

api
django
django-ninja
django-rest-api
django-rest-framework
rest
rest-api
restful-api
Modern REST Logo - Light Modern REST Logo - Dark

Modern REST framework for Django with types and async support!

[![wemake.services](https://img.shields.io/badge/%20-wemake.services-green.svg?label=%20&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC%2FxhBQAAAAFzUkdCAK7OHOkAAAAbUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP%2F%2F%2F5TvxDIAAAAIdFJOUwAjRA8xXANAL%2Bv0SAAAADNJREFUGNNjYCAIOJjRBdBFWMkVQeGzcHAwksJnAPPZGOGAASzPzAEHEGVsLExQwE7YswCb7AFZSF3bbAAAAABJRU5ErkJggg%3D%3D)](https://wemake-services.github.io) [![Modern REST](https://img.shields.io/badge/Modern%20REST-0C4B33?logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTA4MCIgaGVpZ2h0PSIxMDgwIiB2aWV3Qm94PSIwIDAgMTA4MCAxMDgwIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBkPSJNMiA3MDQuMDJMMTQ1LjQ1OSA0NjYuMTlMMjc3Ljg4MyA3MDQuMDJMMTQ1LjQ1OSA5NDEuODQ5TDIgNzA0LjAyWiIgZmlsbD0id2hpdGUiLz4KPHBhdGggZD0iTTE0NS40NTkgOTQxLjg0OUwyIDcwNC4wMkgyNzcuODgzTDE0NS40NTkgOTQxLjg0OVoiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik02NzguOTQ4IDcwNC4wMzVMMzQxLjIzIDEzOEwyMjcuMDcxIDMyOC4yNjRMNDM2LjM2MiA3MDQuMDM1TDMwMy4xNzcgOTQxLjg2NEg1MzYuMjVMNjc4Ljk0OCA3MDQuMDM1WiIgZmlsbD0id2hpdGUiLz4KPHBhdGggZD0iTTY3OC45MzcgNzA0LjAyNkg0MzYuMzVMMzAzLjE2NiA5NDEuODU2SDUzNi4yMzlMNjc4LjkzNyA3MDQuMDI2WiIgZmlsbD0id2hpdGUiLz4KPHBhdGggZD0iTTEwNzguMTcgNzA0LjAzNUw3NDAuNDUxIDEzOEw2MjYuMjkzIDMyOC4yNjRMODM1LjU4MyA3MDQuMDM1TDcwMi4zOTkgOTQxLjg2NEg5MzUuNDcyTDEwNzguMTcgNzA0LjAzNVoiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik0xMDc4LjE3IDcwNC4wMzVIODM1LjU4M0w3MDIuMzk5IDk0MS44NjRIOTM1LjQ3MkwxMDc4LjE3IDcwNC4wMzVaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K&color=35544A)](https://github.com/wemake-services/django-modern-rest) [![test](https://github.com/wemake-services/django-modern-rest/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/wemake-services/django-modern-rest/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/wemake-services/django-modern-rest/branch/master/graph/badge.svg)](https://codecov.io/gh/wemake-services/django-modern-rest) [![PyPI Downloads](https://static.pepy.tech/personalized-badge/django-modern-rest?period=total&units=INTERNATIONAL_SYSTEM&left_color=GREY&right_color=BRIGHTGREEN&left_text=downloads)](https://pepy.tech/projects/django-modern-rest) [![Python Version](https://img.shields.io/pypi/pyversions/django-modern-rest.svg)](https://pypi.org/project/django-modern-rest/) [![wemake-python-styleguide](https://img.shields.io/badge/style-wemake-000000.svg)](https://github.com/wemake-services/wemake-python-styleguide) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/wemake-services/django-modern-rest)

Features

  • [x] Blazingly fast
  • [x] Fully typed and checked with mypy, pyright, and pyrefly in strict modes
  • [x] Strict schema validation of both requests and responses
  • [x] Supports django>=5.2
  • [x] Supports pydantic2, but not bound to it
  • [x] Supports msgspec, but not bound to it
  • [x] Supports async Django without any sync_to_async calls inside
  • [x] Supports openapi 3.1+ schema generation out of the box
  • [x] Supports all your existing django primitives and packages, no custom runtimes
  • [x] Great testing tools with schemathesis, polyfactory, bundled pytest plugin, and default Django's testing primitives
  • [x] 100% test coverage
  • [x] Built by the community for the community, not a single-person project
  • [x] Great docs
  • [x] No AI slop, but built for the LLM era: use our llms-full.txt or context7 for the context, we also support different use-cases specific to LLMs
  • [x] No emojis 🌚️️

Benchmark - Light Benchmark - Dark

Sync mode

Testimonials

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-rest has 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

Installation

Works for: - CPython 3.11+ - Django 5.2+

pip install django-modern-rest

There are several included extras: - 'django-modern-rest[msgspec]' provides msgspec support and the fastest json parsing, recommended to be always included - 'django-modern-rest[pydantic]' provides pydantic support - 'django-modern-rest[jwt]' provides pyjwt auth support - 'django-modern-rest[openapi]' provides OpenAPI schema validation and generates better OpenAPI examples with polyfactory

Example

The shortest example:

>>> 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],
...     Body[UserCreateModel],
...     Headers[HeaderModel],
... ):
...     def post(self) -> UserModel:  # <- can be async as well!
...         """All added props have the correct runtime and static types."""
...         assert self.parsed_headers.consumer == 'my-api'
...         return UserModel(uid=uuid.uuid4(), email=self.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!

License

MIT

Credits

This project was generated with wemake-python-package. Current template version is: e1fcf312d7f715323dcff0d376a40b7e3b47f9b7. See what is updated since then.

Stars
822
36.25% more than last month
Forks
89
Open Issues
25