Automagically import settings from installed applications

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# [...]

# this is an example!
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    # my apps:
    'myapps.blog',
    'myapps.chat',
    'myapps.forum',
)

APPS_BASE_NAME = 'myapps'

# Import Applicaton-specific Settings
for app in INSTALLED_APPS:
    if app.startswith(APPS_BASE_NAME):
        try:
            app_module = __import__(app, globals(), locals(), ["settings"])
            app_settings = getattr(app_module, "settings", None)
            for setting in dir(app_settings):
                if setting == setting.upper():
                    locals()[setting] = getattr(app_settings, setting)
        except ImportError:
            pass

More like this

  1. Dynamic import from an installed app by Archatas 4 years, 2 months ago
  2. Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 4 years, 10 months ago
  3. Cacheable resources by jbrisbin 4 years, 10 months ago
  4. Scoped Cache Compatible with Django Caching Helpers by axiak 5 years, 3 months ago
  5. Breaking tests.py into multiple files by gsakkis 3 years, 2 months ago

Comments

Archatas (on February 11, 2008):

Isn't it a better approach to set the default values in the app and overwrite them in the main settings.py when necessary? For example:

blog/model.py

from django.conf import settings
TEST_SETTING = getattr(settings, "TEST_SETTING", "default value")

#

(Forgotten your password?)