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")

More like this

  1. TestSettingsManager: temporarily change settings for tests by carljm 4 years, 9 months ago
  2. Serve static media files from app/media subdirectory by adamlofts 4 years, 10 months ago
  3. module_from_path by simon 5 years, 1 month ago
  4. Conditional url pattern include by zbyte64 2 years, 10 months ago
  5. djangopath: conveniently set sys.path and DJANGO_SETTINGS_MODULE by alia_khouri 4 years, 10 months ago

Comments

ClaytonHope (on September 3, 2011):

It's perfect that we can receive the mortgage loans and this opens new opportunities.

#

(Forgotten your password?)