Skip to main content

Integrate injector with Django

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_injector-0.3.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file django_injector-0.3.0.tar.gz.

File metadata

  • Download URL: django_injector-0.3.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for django_injector-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bac43ee051dad2aa4aab4556c069be0898e3737472576369fee05c78830aa865
MD5 6d1f7a8f191d56207dc9586e6a96557a
BLAKE2b-256 a561f3b02dc2979c4e45498c8866b821cf12ab26e940e51e692bd802f0221351

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page