github wemake-services/django-modern-rest 0.14.0
Version 0.14.0

4 hours ago

Features overview

1. ty official support

We now officially support ty type-checker!

2. Router.to_urlpatterns method was added

Now you including routers into the final urlpatterns is easier:

# urls.py
from django.urls import include

from dmr.routing import Router, path
from myapp.views import UserController

router = Router(
    'api/',
    [
        path('user/', UserController.as_view(), name='users'),
    ],
)

urlpatterns = [
    router.to_urlpatterns(namespace='api'),
]

Docs: https://django-modern-rest.readthedocs.io/en/latest/pages/routing.html

3. @validate now supports reusable controllers

You can now use @validate with abstract controllers and type variables.
Just like @modify could before this release.

from abc import abstractmethod
from http import HTTPStatus
from typing import Generic, TypeVar

from django.http import HttpResponse

from dmr import Body, Controller, ResponseSpec, validate
from dmr.serializer import BaseSerializer
from dmr.types import safe_typevar

_SerializerT = TypeVar('_SerializerT', bound=BaseSerializer)
_RequestModelT = TypeVar('_RequestModelT')
_ResponseBodyT = TypeVar('_ResponseBodyT')


class ReusableController(
    Controller[_SerializerT],
    Generic[_SerializerT, _RequestModelT, _ResponseBodyT],
):
    @validate(
        ResponseSpec(
            safe_typevar('_ResponseBodyT'),
            status_code=HTTPStatus.CREATED,
        ),
    )
    def post(self, parsed_body: Body[_RequestModelT]) -> HttpResponse:
        return self.to_response(self.convert(parsed_body))

    @abstractmethod
    def convert(self, parsed_body: _RequestModelT) -> _ResponseBodyT:
        raise NotImplementedError

Now, you can choose which decorator to use, they offer the same semantics for this feature.
See further docs: https://django-modern-rest.readthedocs.io/en/latest/pages/reusable-code.html#real-endpoints-support

Migration prompt

It covers all the breaking changes.

You are migrating a Python project from `django-modern-rest` 0.12.0 to 0.13.0.
Load the latest documentation from https://django-modern-rest.readthedocs.io/llms-full.txt
before making any changes.

Apply **all** of the following breaking changes to the codebase.
For each change, search the entire project (including tests, fixtures, and
any helper modules) before editing.

---

### 1. `dmr.routing.ExternalURL``dmr.routing.external_path`

`ExternalURL` was made protected, it should not be ever used directly by users.
Users must switch to `external_path` function API. Change the `ExternalURL` + `path()`
to use `external_path` API with the same arguments.

What's Changed

Breaking changes

  • Refactored public routing.ExternalURL into protected _ExternalURL,
    use external_path() function instead, #1262

Features

  • Added initial ty support, #1257
  • Added support of reusable controllers with @validate, #1259
  • Added default value to prefix parameter in Router.__init__, #1267
  • Added to_urlpatterns function to include Router
    instances into urlpatterns or other routers, #1262

Bugfixes

  • OpenAPIConfig.openapi_version now support any str argument, #1252

Misc

  • Improve reusable controllers docs, #1259

New Contributors

Full Changelog: 0.13.0...0.14.0

Don't miss a new django-modern-rest release

NewReleases is sending notifications on new releases.