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
Solution 1:
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
Solution 2:
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))
Post a Comment for "Get Own Applications In Django"