Get a list of all installed applications in Django and their attributes

Solution 1:

Under Django 1.7 and above (thanks Colin Anderson):

from django.apps import apps
apps.get_models()

Under Django 1.6 and below.

If you want all models, try:

from django.db.models import get_models

for model in get_models():
   # Do something with your model here
   print model.__name__, [x.name for x in model._meta.fields]

I believe the older function still works.

Solution 2:

[edit]

Since Django 1.7, accessing settings.INSTALLED_APPS is discouraged: "Your code should never access INSTALLED_APPS directly. Use django.apps.apps instead." – johanno

So the blessed way is:

from django.apps import apps

for app in apps.get_app_configs():
    print(app.verbose_name, ":")
    for model in app.get_models():
        print("\t", model)

Older version of this answer:

All applications are registered in the settings.py file.

In [1]: from django.conf import settings

In [2]: print(settings.INSTALLED_APPS)
['django.contrib.auth', 'django.contrib.contenttypes', 
 'django.contrib.sessions', 'django.contrib.sites', 
 'django.contrib.messages', 'django.contrib.staticfiles',
 'django.contrib.admin', 'raven.contrib.django']

You can import each application and list their attributes:

In [3]: from pprint import pprint

In [4]: for app_name in settings.INSTALLED_APPS:
    try:
        module_ = __import__(app_name)
    except ImportError:
        pass
    map(print, ['=' * 80, "MODULE: "+app_name, '-' * 80])
    pprint(module_.__dict__)

In order to use the new print function instead of the print statement in older Python you may have to issue a from __future__ import print_function (or just change the line containing the print call).

Solution 3:

You can retrieve installed apps like that (in interpreter) :

>>> from django.conf import settings
>>> [app for app in settings.INSTALLED_APPS
     if not app.startswith("django.")]
['myapp1', 'myapp2', 'myapp3']

Solution 4:

The list of installed applications is defined in settings.INSTALLED_APPS. It contains a tuple of strings, so you can iterate on it to access each application's name.

However, I'm not sure what you mean by each application's attributes and fields.

Solution 5:

To get the actual apps themselves (not just names), this is what I came up with:

from django.conf import settings
from django.utils.module_loading import import_module
apps = [import_module(appname) for appname in settings.INSTALLED_APPS]

Though you may want to do some error handling, or filtering.