Django Troubleshooting¶
Common issues and fixes for Wireup's Django integration.
@inject fails outright in request handlers¶
Symptom:
- Request handler injection fails immediately.
- Errors mention missing request scope or
django.http.HttpRequest ... only available during a request.
Fix:
- Ensure middleware is configured:
settings.py
MIDDLEWARE = [
"wireup.integration.django.wireup_middleware",
# ...existing middleware...
]
- Keep Wireup middleware near the start of the middleware list.
- Use
@injectonly for request-scoped callables. For non-request callables, use@inject_app.
Unknown service requested¶
Symptom:
UnknownServiceRequestedErrorfor a type you expected to be injectable
Fix:
- Add the module containing the injectable to
WIREUP.injectables. - Ensure the class/function is marked with
@injectable.
settings.py
WIREUP = WireupSettings(
injectables=["mysite.services"],
auto_inject_views=False,
)
DRF or Ninja endpoint is not injecting¶
Symptom:
- Endpoint runs, but injected parameter is missing or treated as a normal arg
Fix:
- Add
@injectexplicitly on DRF/Ninja handlers.
@api_view(("GET",))
@inject
def drf_view(request: Request, service: Injected[MyService]) -> Response: ...
Using the wrong decorator¶
Rule:
- Use
@injectfor request handlers. - Use
@inject_appfor non-request callables (commands, signals, checks, scripts).
If a callable runs outside HTTP request scope, switch from @inject to @inject_app.
Async dependency in sync context¶
Symptom:
- Error indicates an async dependency cannot be created in a sync context
Fix options:
- Use async handlers where async dependencies are injected.
- Make the dependency sync if async behavior is not required.
- For app-level flows, structure execution to await async work explicitly.
Auto-injection confusion¶
If behavior is inconsistent between core Django and DRF/Ninja, set explicit mode:
settings.py
WIREUP = WireupSettings(
injectables=["mysite.services"],
auto_inject_views=False,
)
Then use @inject explicitly in request handlers.