Search code examples
pythondjangopython-3.xdjango-apps

Iterating over a subset of Apps in Django


As part of a Django project, my user will be able to select from a variety of options. The functionality for each option is stored in a separate app. My project structure looks something like this:

MyProject/
    MyProject
    App1
    App2
    Options/
        Option1/
        Option2/
        Option3/
        …
    …
    manage.py

Option1, Option2 etc. is each an App in its own right, and listed in INSTALLED_APPS My question is: Is there a way of iterating over each app contained within the options directory. For example suppose each app contained a file called utils.py with a function called foo in each utils.py.

Is there a way of doing something like this:

from .. import Options
survey = [ app.utils.foo() for app in Options]

Failing this, is there a way to just iterate over all apps? Is there something I can do with django.apps perhaps?


Solution

  • Depending on how you've configures your apps.py

    from django.apps import apps
    
    for name, app in apps.app_configs.items():
        if name in ['OPTIONS-N']:
            utils = app.module.utils