Django Integration¶
-
Inject Beyond Views
Inject in core Django views, DRF handlers, Ninja endpoints, middleware helpers, decorators, and other request-time call sites.
-
App-Level Entry Points
Use
@inject_appfor management commands, Django 6 background tasks, signals, checks, and scripts outside request scope. -
Request Context in Services
Inject
HttpRequestand other scoped services into your application code without threading request objects through every layer. -
Django, DRF, and Ninja
Use one DI model across core Django, Django REST framework, and Django Ninja while keeping your service layer framework-agnostic.
Wireup integrates with Django at both request scope and application scope. Use @inject for request-time callables and
@inject_app for non-request entry points, while keeping services as ordinary Python classes that are easy to test and
reuse.
Quick Start¶
This is the shortest path to a working endpoint with explicit injection.
from wireup.integration.django import WireupSettings
INSTALLED_APPS = [
# ...existing apps...
"wireup.integration.django",
]
MIDDLEWARE = [
"wireup.integration.django.wireup_middleware",
# ...existing middleware...
]
WIREUP = WireupSettings(
injectables=["mysite.services"],
auto_inject_views=False,
)
from wireup import injectable
@injectable
class GreeterService:
def greet(self, name: str) -> str:
return f"Hello {name}"
from django.http import HttpRequest, HttpResponse
from wireup import Injected
from wireup.integration.django import inject
from mysite.services import GreeterService
@inject
def greet(
request: HttpRequest, greeter: Injected[GreeterService]
) -> HttpResponse:
return HttpResponse(greeter.greet(request.GET.get("name", "World")))
from django.urls import path
from mysite.views import greet
urlpatterns = [
path("greet/", greet),
]
Run the app with python manage.py runserver, then open http://127.0.0.1:8000/greet/?name=World.
Detailed Guides¶
- Django Setup and Installation: installation, middleware placement, and settings/config integration.
- Inject in Views: core Django, DRF, Ninja, forms, and request-scoped patterns.
- Request-Time Injection: reusable decorators, middleware entry points, and direct container access.
- App-Level Injection: management commands, Django 6 background tasks, signals, checks, and scripts with
@inject_app. - Django Testing:
Client,AsyncClient,call_command, and override patterns. - Troubleshooting: common errors and quick fixes.