Can dependency injection be used with django?
I am very popular with using the technique of dependency injection to decouple my code. This would typically involve providing an object with functionality, into the constructor of it's dependent.
For the first time, I am using django to make a web api (with a database of objects attached). I intended to inject an elaborate dependency into the otherwise simple method. (in my case it was functionality to interpret messages coming from RabbitMQ exchanges, but my minimal example is just interpreting a generic message as a site-dependent dictionary).
However in django everything seems to be autogenerated from either static methods or the definition of classes, I couldn't find where anything was actually instantiated or customisable to push the dependency in.
Is the technique and the django framework just incompatible or am I missing something?
Code so far
(minimal example recreation, not actual code)
in urs.py:
urlpatterns = [
path("run/", views.run),
]
in views.py
def run(request):
interpreter = AbstractDataInterpreter() #This is the object I want to inject
data = interpreter.interpret(request)
return JsonResponse(data, safe=False)
I have a test class
TestDataInterpreter
to use for testing.
I have a class
CustomDataInterpreter
custom for my domain/ecosystem.
I plan for for other interpreters on different deployments going forward.
But I can't find the mechanism to actually inject an interpreter into the run command on the different deployments.