Saturday, August 30, 2014

Re: how to get a list of all installed applications



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




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ee48eaaa-a507-4d54-a6ef-d3fa57503234%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

1 comment:

  1. Nice blog. Writing work is done with exceptional quality. I think the writing skills are the required skills wherever necessary.
    Thanks
    Resell dedicated servers

    ReplyDelete