how to get a list of all installed applications

2,894 views
Skip to first unread message

Stefan Foulis [spiderware gmbh]

unread,
May 20, 2007, 12:44:15 PM5/20/07
to django...@googlegroups.com
I'm working on a little Navigation/Sitemap-ish application that will
provide a way to define the sitemap similar to urls.py, but with way
more options. based on that data the application will automatically
create the necessary urlpatterns und provide a facility to make nice
navigations including highlighting of the currently selected
navigation items.... so much for the intro :-)

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

Malcolm Tredinnick

unread,
May 20, 2007, 12:51:49 PM5/20/07
to django...@googlegroups.com
On Sun, 2007-05-20 at 18:44 +0200, Stefan Foulis[spiderware gmbh] wrote:
[...]

> 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.

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

Cj Welborn

unread,
Aug 30, 2014, 2:25:51 PM8/30/14
to django...@googlegroups.com, mal...@pointy-stick.com


On Sunday, May 20, 2007 11:51:49 AM UTC-5, Malcolm Tredinnick wrote:

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


Hello, this is a very old post but seeing how I got here from a Google search I figure someone else might also. Looking at get_apps() this is what I get:

from django.db.models import loading
   
for mod in loading.get_apps():
       
print(mod.__name__)


And the output:
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...>


So you see, get_apps() does return the models, not the apps themselves.

However, I did find this that works:
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))


Or to shorten it:
from django.conf import settings
from django.utils.module_loading import import_module
apps
= [import_module(appname) for appname in settings.INSTALLED_APPS]


I needed it to design a search app for my site. If anyone has a better way I would really like to hear about it (no sarcasm).




Collin Anderson

unread,
Aug 30, 2014, 7:02:57 PM8/30/14
to django...@googlegroups.com, stefan...@spiderware.ch
The soon-to-be-released version 1.7 has a documented API for accessing all models and apps:

Cj Welborn

unread,
Aug 31, 2014, 6:52:48 PM8/31/14
to django...@googlegroups.com, stefan...@spiderware.ch
On Saturday, August 30, 2014 6:02:57 PM UTC-5, Collin Anderson wrote:
The soon-to-be-released version 1.7 has a documented API for accessing all models and apps:


That's awesome, I'll be on the lookout for it. 
Reply all
Reply to author
Forward
0 new messages