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'
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()
Ok, I know, the above code looks like overkill to instantiate the
object specified in the setting. But I could not find anyway to do
that properly in Python. The problem is that the above code actually
works, but only if the module 'forms.py' containing the object
(MyModelForm) has been compiled to forms.pyc
If that 'forms' module is never referenced anywhere with an import
statement, then it is not compiled, and therefore the above code
generates an AttributeError: 'module' object has no attribute
'MyModelForm'
I suspect there might be a way in Python to instantiate a class using
its full name (including parent modules) and to compile the .py module
if it hasn't already been compiled.
Any idea?
Thanks a lot!
Julien
On Feb 13, 12:48 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Tue, 2008-02-12 at 05:35 -0800, Julien wrote:
> > Hi there,
>
> > I'm trying import a class definition in settings.py with:
> > from myapp.models import MyModel
>
> Oh, don't do that. Very bad. :-(
>
> Importing models means they start to get registered with the app_cache
> (to track reverse relations, amongst other things), which means we need
> to know about other applications, which is specified in a setting, which
> is in the settings file. Spot the circular dependency.
>
> In settings.py, you can't really expect to use anything from Django
> itself. You can write arbitrary Python code that talks to other parts of
> your system or whatever, but large portions of the core of Django
> requires settings to have been fully imported before they can be used.
> So you cannot use them inside settings itself.
>
> Perhaps if you explain what you're trying to achieve we could suggest an
> alternate route.
>
> Regards,
> Malcolm
>
> --
> I don't have a solution, but I admire your
> problem.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
-~----------~----~----~----~------~----~------~--~---