alexandernst
alexandernst

Reputation: 15099

Get own applications in Django

Is there a way I can get a list of the applications that belong to a Django project itself (ignoring the apps that are installed with pip).

Said with other words: can I exclude the apps that were installed with pip from settings.INSTALLED_APPS?

Upvotes: 2

Views: 119

Answers (2)

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8398

You can get all django apps using django.apps,

In [35]: import django.apps

In [36]: models = django.apps.apps.get_models()

In [37]: myapps = set([x.__module__.split('.')[0]  for x in models])

myapps will give you all own django applications.

You can get all the models from myapps using,

In [89]: for o in myapps:
    ...:     try:
    ...:         x = django.apps.apps.get_app_config(o)
    ...:         print x.models
    ...:     except LookupError:
    ...:         pass

Upvotes: 3

Alasdair
Alasdair

Reputation: 309089

You can loop through the apps using get_app_configs, then check the path attribute to see if they are in your project directory.

>>> from django.apps import apps
>>> for a in apps.get_app_configs():
...     if a.path.startswith("/path/to/project/"):
...         print("%s is in project directory" % a.name)
...     else:
...         print("%s is in %s" % (a.name, a.path))

Upvotes: 2

Related Questions