Skip to main content

Django Injector

Add Injector to Django.

Injector is a dependency-injection framework for Python, inspired by Guice. You can find Injector on PyPI and Injector documentation on Read the Docs. Django-Injector is inspired by Flask-Injector.

Django Injector support Django 3.2+ running on Python 3.7+ or Pypy 3.7+.

Django-Injector is compatiable with CPython 3.6+, Django 2.2+ and Django Rest Framework 3 (optional).

Github page: https://github.com/blubber/django_injector

PyPI package page: https://pypi.org/project/django-injector/

Features

Django-Injector lets you inject dependencies into:

  • Views (functions and class-based)
  • Django template context processors
  • Rest Framework views (functions and class-based)
  • Rest Framework view sets

Injector uses Python type hints to define types.

Installation

Django-Injector can be installed with pip:

pip install django_injector

After installation add django_injector to your INSTALLED_APPS and optionally enable the request injection middleware.

INSTALLED_APPS = [
    ...
    'django_injector',
]

MIDDLEWARES = [
    ...
    'django_injector.inject_request_middleware',
]

The middleware enables the injection of the current request.

WARNING: The injection middleware only works on WSGI application containers. Enabling the middleware on an ASGI container (like daphne) will result in an error.

Example

from django.views.generic import View
from injector import inject
from rest_framework.views import APIView


class MyService:
    ...

def my_view(request, my_service: MyService):
    # Has access to an instance of MyService
    ...

class MyView(View):
    @inject
    def __init__(self, my_service: MyService):
        # Class based views require the @inject decorator to properly work with
        # Django-Injector. The injection also works on the setup method.
        self.my_service = my_service

class MyAPIView(APIView):  # Also works on ViewSet derivatives
    @inject
    def setup(self, request, my_service: MyService, **kwargs):
      # Injection on __init__ will result in a TypeError when using the HTML
      # renderer.

Context processors have the same signature as view functions and work in the same way. They should be registered in the template options as usual.

It is also possible to use injection in management commands. In this case the injection is done into the __init__ method:

from django.core.management import BaseCommand

from injector import inject

class Command(BaseCommand):

    @inject
    def __init__(self, my_service: MyService):
        self.my_service = my_service
        super().__init__()

Injector Module support

Django Injector supports Injector modules, just add a INJECTOR_MODULES setting to your configuration with a list of dotted paths to modules you want to load. The modules should either be callables that accept a single argument, binder or subclasses of injector.Module. The modules will be loaded when the injector Django app loads in the order they appear in the list. For example:

INJECTOR_MODULES = [
    'my_app.modules.configure_for_production',
    'my_app.modules.ServiceModule',
]

DjangoModule

Django Injector comes with a built-in module named DjangoModule that is always loaded as the first module. It provides a couple of bindings for Django built-in types, namely:

  • django.htt.HttpRequest: The current request. This only works if django_injector.inject_request_middleware is loaded and the application runs in a WSGI container.
  • django.conf.Settings: Injects django.conf.settings.

Metadata

Release files for django-injector 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-injector 0.3.0
File Size Uploaded
django_injector-0.3.0.tar.gz 4.0 kB Details

Release files / django_injector-0.3.0.tar.gz

Download URL django_injector-0.3.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
bac43ee051dad2aa4aab4556c069be0898e3737472576369fee05c78830aa865
BLAKE2b-256 checksum
How to use checksums
a561f3b02dc2979c4e45498c8866b821cf12ab26e940e51e692bd802f0221351
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

0.3.1

1 release file

This release

0.3.0 This release

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

2 release files

0.1.0

2 release files

0.0.2

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page