[Django]-Dynamic Loading of Modules then using "from x import *" on loaded module

4👍

At from models import * you are NOT referring to the models variable. You are just trying to import module called ‘models’, which, obviously, does not exist.

You can use hack like this to import everything from the module into current namespace:

ldict = locals()
for k in models.__dict__:
    if not k.startswith('__') or not k.endswith('__'):
        ldict[k] = models.__dict__[k]

Or use exec() to load the module,

exec("from project.data_{0}.models import *".format(release)) in locals()

Leave a comment