Hi,
Thanks for the tip. However I still get an ImportError: "No module
name MyModel".
I tried the following:
klass = __import__(settings.MY_MODEL_FORM, {}, {}, '')
klass = __import__(settings.MY_MODEL_FORM, {}, {}, [''])
klass = __import__(settings.MY_MODEL_FORM, globals(), locals(), [''])
I even tried to hard code it:
klass = __import__("myapp.forms.MyModelForm", {}, {}, [''])
But I still get that ImportError. What is strange is that if I do:
from myapp.forms import MyModelForm
... it works... so the module should obviously be visible and found.
What am I missing? I looked in the Django code, and indeed, __import__
is used in a lot of places, and exactly in the way that you have
suggested. I just don't understand why it does not work for me :(
Am I missing something?
Many thanks,
Julien
On Feb 13, 1:17 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Tue, 2008-02-12 at 06:08 -0800, Julien wrote:
> > Oh, I see!! Thanks for the explanation.
>
> > So, what I'm trying to achieve is to specify a model class as a
> > setting that can be used in some views.
>
> > As you've said, doing "from myapp.forms import MyModelForm" is not
> > possible, so I tried:
>
> > MY_MODEL_FORM = 'myapp.forms.MyModelForm'
>
> Now you see why all the Django settings like this are strings. :-)
>
> > And then, in some view I do:
>
> > from django.conf import settings
> > splitted = settings.MY_MODEL_FORM.split('.')
> > module = __import__(splitted[0])
> > for i in range(len(splitted)-1):
> > module = getattr(module, splitted[i+1])
> > my_model_form_class = module
> > my_model_form = my_model_form_class()
>
> You're certainly on the right track. However, you're not using the full
> power of __import__(). Have a read of the Python docs (in the Library
> Reference, have a look at builtin functions. It's the first entry in
> ยง2.1). Basically, the trick is that you can pass a dotted string form as
> the first argument to __import__. However, you also need to watch out
> for what __import__ returns (explained in the docs). So, typically,
> you'll be doing this:
>
> klass = __import__(settings.MY_MODEL_FORM, {}, {}, '')
>
> where I've used "klass" instead of "my_model_form_class". Passing '' as
> the last argument means that klass will now be a reference to what you
> expect (try leaving it off and look at what klass is, if you're
> interested).
>
> Not surprisingly, Django does this sort of thing all over the place.
> See, for example, django/template/loader.py or
> django/db/models/loader.py (just search for __import__). It's the
> standard way to import something dynamically on those rare occasions
> when you need to.
>
> Regards,
> Malcolm
>
> --
> Plan to be spontaneous - tomorrow.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---