Skip to content

Modern REST framework for Django with types and async support!

License

golovindev/django-modern-rest

 
 

Repository files navigation

Modern REST Logo - Light Modern REST Logo - Dark

Modern REST framework for Django with types and async support!

wemake.services test codecov Python Version wemake-python-styleguide

Features

  • Blazingly fast
  • Fully typed and checked with mypy and pyright in strict modes
  • Strict schema validation of both requests and responses
  • Supports pydantic2, but not bound to it
  • Supports msgspec, but not bound to it
  • Strict schema validation for requests and responses
  • Supports async Django
  • Supports openapi 3.1+ schema generation out of the box
  • Supports all your existing django primitives and packages
  • Great testing tools with schemathesis, polyfactory, bundled pytest plugin, and default Django's testing primitives
  • 100% test coverage
  • No emojis 🌚️️

Installation

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

Example

The shortest example:

>>> import uuid
>>> import pydantic
>>> from django_modern_rest import Body, Controller, Headers
>>> # Or use `django_modern_rest.plugins.msgspec` or write your own!
>>> from django_modern_rest.plugins.pydantic import PydanticSerializer

>>> class UserCreateModel(pydantic.BaseModel):
...     email: str

>>> class UserModel(UserCreateModel):
...     uid: uuid.UUID

>>> class HeaderModel(pydantic.BaseModel):
...     token: str = pydantic.Field(alias='X-API-Token')

>>> 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.token == 'secret!'
...         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 django_modern_rest import Router

>>> router = Router([
...     path('user/', UserController.as_view(), name='users'),
... ])
>>> urlpatterns = [
...     path('api/', include((router.urls, 'your_app'), namespace='api')),
... ]

Done! Now you have your shiny API with 100% type safe validation and interactive docs.

The full documentation has everything you need to get started!

License

MIT

Credits

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

Feedback

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

About

Modern REST framework for Django with types and async support!

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.1%
  • Other 0.9%