I've used django.db.models.get_apps() to get all the models for all
installed applications before. Is there a similar method somewhere in
django to get all the installed applications.
(I want to search through all installed applications and check if
there is a sitemap.py defined... and load data from there to build a
global navigation tree)
Of course I could get the list of strings from settings.py and work
on from there, but I was wondering if there is a method that
actually returns a list of callable python modules....
any ideas?
many thanks
stefan
Have a look in django/db/models/loading.py . There are a number of
useful methods in there. In particular, get_apps(), which contrary to
what you claim above, should give you all installed apps, not installed
models (you use get_models(app_name) to get all the models for an app).
Regards,
Malcolm
Have a look in django/db/models/loading.py . There are a number of
useful methods in there. In particular, get_apps(), which contrary to
what you claim above, should give you all installed apps, not installed
models (you use get_models(app_name) to get all the models for an app).Regards,
Malcolm
from django.db.models import loading
for mod in loading.get_apps():
print(mod.__name__)
django.contrib.auth.models
django.contrib.contenttypes.models
django.contrib.sessions.models
django.contrib.sites.models
django.contrib.messages.models
django.contrib.staticfiles.models
django.contrib.admin.models
django.contrib.admindocs.models
debug_toolbar.models
django_extensions.models
solo.models
wp_user_agents.models
home.models
projects.models
# <...plus all of my other app's models...>
from django.conf import settings
from django.utils.module_loading import import_module
apps = []
for appname in settings.INSTALLED_APPS:
apps.append(import_module(appname))
from django.conf import settings
from django.utils.module_loading import import_module
apps = [import_module(appname) for appname in settings.INSTALLED_APPS]
The soon-to-be-released version 1.7 has a documented API for accessing all models and apps: