Declare models elsewhere than in "models.py"

Posted by sebpiq on Stack Overflow See other posts from Stack Overflow or by sebpiq
Published on 2010-04-29T12:34:48Z Indexed on 2010/04/29 12:37 UTC
Read the original article Hit count: 254

Filed under:
|

Hi ! I have an application that splits models into different files.

Actually the folder looks like :

>myapp
    __init__.py
    models.py
    >hooks
        ...
    ...

myapp don't care about what's in the hooks, folder, except that there are models, and that they have to be declared somehow. So, I put this in myapp.__init__.py :

from django.conf import settings

for hook in settings.HOOKS :
    try :
        __import__(hook)
    except ImportError as e :
        print "Got import err !", e
#where HOOKS = ("myapp.hooks.a_super_hook1", ...) 

The problem is that it doesn't work when I run syncdb(and throws some strange "Got import err !"... strange considering that it's related to another module of my program that I don't even import anywhere :/ ) !

So I tried successively :

1)

for hook in settings.HOOKS :
    try :
        exec ("from %s import *" % hook)

doesn't work either : syncdb doesn't install the models in hooks

2)

from myapp.hooks.a_super_hook1 import *

This works

3)

exec("from myapp.hooks.a_super_hook1 import *")

This works to

So I checked that in the test 1), the statement executed is the same than in tests 2) and 3), and it is exactly the same ...

Any idea ???

© Stack Overflow or respective owner

Related posts about django

Related posts about python