Wireup for Django: one DI model across views, DRF, commands, and Django 6 tasks

Hi all,

I built Wireup, a type-driven DI library for Python, and I recently spent some time improving the Django integration.

The integration goes beyond DI in views, and lets you inject dependencies in request handlers, into any function inside the request path, management commands, django 6 background tasks, celery etc.

What you get:

  • startup graph validation for missing dependencies, cycles, lifetime mismatches, and missing config so you can catch wiring errors immediately instead of the first time a dependency is created
  • one service graph reusable across Django views, DRF, Ninja, commands, and background tasks
  • regular Python classes that stay easy to test outside of the container

Simple view example:

from django.http import HttpRequest, HttpResponse
from wireup import Injected
from wireup.integration.django import inject

@inject
def greet(request: HttpRequest, greeter: Injected[GreeterService]) -> HttpResponse:
    return HttpResponse(greeter.greet(request.GET.get("name", "World")))

For injections outside the request path, use @inject_app instead of @inject:

from django.core.management.base import BaseCommand
from wireup import Injected
from wireup.integration.django import inject_app

class Command(BaseCommand):
    @inject_app
    def handle(self, *args, greeter: Injected[GreeterService], **options) -> None:
        self.stdout.write(greeter.greet("World"))

Django 6 background tasks are also supported:

from django.tasks import task
from wireup import Injected
from wireup.integration.django import inject_app

@task
@inject_app
def send_welcome_email(user_id: int, email: Injected[EmailService]) -> None:
    email.send_welcome(user_id)

If a project also uses Celery, Wireup also comes with a Celery integration.

Docs: Django Integration - Wireup
Repo: GitHub - maldoinc/wireup: Type-driven dependency injection for Python. Fail-fast validation, explicit lifetimes, native integrations for FastAPI, Flask, Django, and more. ยท GitHub

Happy to hear thoughts and feedback on this and how others are handling DI in larger Django projects.

1 Like