Dynamic import from an installed app

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.db import models
from django.utils import importlib

# get the installed models.py from the installed app
app = models.get_app("myapp")

# import the views from the same app
views = importlib.import_module(app.__name__[:-6] + "views")

# import templatetags from the same app
mytemplatetags = importlib.import_module(app.__name__[:-6] + "templatetags.mytemplatetags")

# import any other module from the same app
myothermodule = importlib.import_module(app.__name__[:-6] + "myothermodule")

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.