Interested in using inject with Django

94 views
Skip to first unread message

narsciscott

unread,
Jun 10, 2011, 12:12:26 PM6/10/11
to Python Inject
Thank you for Python Inject. I am just getting started with it, but it
looks like it is nicely done.

I would really appreciate any insight into how to use inject properly
with Django. Where can I setup my Injector configuration in a Django
project for subsequent dependency injection into my views? I have
tried doing this in the settings.py file and in the project and
application __init__ files to no avail. The only thing that works for
me is to do the setup in the view module itself.

I have a view that looks something like this:

class MyView(View):

def get(self, request, controller_name):
clz = injector.get_instance(controller_name)
return HttpResponse(clz.view())

where the injector setup looks like this:
injector = inject.Injector()
inject.register(injector)
injector.bind(controller_name, to=ControllerClass,
scope=inject.somescope)

I have tried this setup with both appscope, and with reqscope using
the Django inject middleware. I have tried it in both the settings.py
file, and in the __init__ files for the project and for the
application. But I just get NoProviderError in every case when I call
get_instance in the view. The only way it works for me is to do the
Injector setup in the view module -- which really kind of defeats the
purpose. Perhaps I am not really understanding what the middleware is
supposed to be doing regarding reqscope, or what the true scope of
appscope should be.

So, my question is, in Django, either in appscope or in reqscope (the
latter using the middleware), where should I be setting up my Injector
for subsequent injection into my views?

Thank you

Ivan Korobkov

unread,
Jun 10, 2011, 12:17:45 PM6/10/11
to python...@googlegroups.com
Hi, I don't have time now. I will answer tomorrow.
--
Ivan Korobkov
ivan.k...@gmail.com

Ivan Korobkov

unread,
Jun 14, 2011, 12:24:12 PM6/14/11
to python...@googlegroups.com
Hello, I'm very sorry for the delay.

First, thank you for being interested in python-inject. I hope it will be
a pleasure for you to use it.

I recommend to use the second version of python-inject. It has not been
released only because I haven't updated the documentation, yet it is
stable and is extensively used in my projects and my colleagues' projects.

You can get it from github from the v2.0-providers branch:
git clone git://github.com/ivan-korobkov/python-inject.git -b v2.0-providers

You can find a working django example in the examples directory.

>>> Where can I setup my Injector configuration in a Django
project for subsequent dependency injection into my views?
I prefer to setup it in a special file bindings.py which is imported
at the end of settings.py.

I'm sorry but I do not understand why you need to get a controller
from the injector. Yet, you'd bettor not import the injector instance
from anywhere, you should the inject.get_instance method.

import inject
class MyView(View):

   def get(self, request, controller_name):
       controller_instance = inject.get_instance(controller_name)
       return HttpResponse(controller_instance.view())

The request scope is used to store instances specific to each
request. DjangoInjectMiddleware creates a thread-local
dictionary to store instances at the start of a request,
and deletes it at the end.

I recommend you to look at the provided django and simple
examples in the source code.

Sincerely yours, Ivan Korobkov

narsciscott

unread,
Jun 14, 2011, 4:32:12 PM6/14/11
to Python Inject
Thanks Ivan.

At a glance, it looks like my primary problem is that I am re-
instantating an Injector and trying to get my dependencies from there
instead of directly from inject.get_instance, as you mentioned. I see
why this would not work now that you mention it.

I will look at this in more detail when I get a chance. I've had to
move on to some other things for the moment.

Thank you for your help,
Scott
> > ivan.korob...@gmail.com
Reply all
Reply to author
Forward
0 new messages