This package provides integration of Dishka dependency injection framework and Modern REST framework for Django with types and async support!
- Automatic REQUEST and SESSION scope management using middleware
- Easy setup and integration with Django Modern REST framework
- Support for both async and sync views
- Support for Django views
pip install dmr-dishkauv add dmr-dishka- Import
from dishka import Provider, provide, Scope
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, request: Request) -> X:
...- Create provider.
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self) -> X:
...- Mark those of your handlers parameters which are to be injected with
FromDishka[]and decorate them using@inject
from dmr_dishka.integration import inject
class ExampleBlueprint(Controller[MsgspecSerializer]):
@inject
async def get(self, x: FromDishka[X])
...- Make container
container = make_async_container(YourProvider())- Setup dishka integration in your
asgi.py
# asgi.py
from dishka import make_async_container
from django_example.app.ioc import AppProvider
from dmr_dishka.integration import setup_dishka
container = make_async_container(AppProvider())
setup_dishka(container)- Steps 1 and 2 is identical to async
- In step 3 you need to decorate your handlers with
@inject_syncand their parameters should be marked withFromDishka[]as well
from dmr_dishka.integration import inject_sync
class ExampleBlueprint(Controller[MsgspecSerializer]):
@inject_sync
def get(self, x: FromDishka[X])
...- Step 4 is identical to async
- In step 5 need to setup dishka in your
wsgi.pyinstead ofasgi.py
# wsgi.py
from dishka import make_container
from django_example.app.ioc import AppProvider
from dmr_dishka.integration import setup_dishka
container = make_container(AppProvider())
setup_dishka(container)| Check src/django_example for more examples of usage and vanila django view support.