Login

Django Settings Splitter & Local Settings loader

Author:
rudude
Posted:
March 2, 2014
Language:
Python
Version:
1.6
Score:
1 (after 1 ratings)

Everybody know about long spagetty-style settings file for django :-)

I tried to find any cool settings loader, but have no luck.I created this one myself.

Ok, we forgetting about settings.py and creating module settings (folder named settings with file __init__.py).

This __init__.py file have preloader for modules placed in settings folder and ../settings_local.py (if exists) at the end. settings_local is awesome tool, when you use any VCS like git and have settings in vcs, but for example you have different database connection settings. You can change this settings in settings_local.

Settings splitter have variable moduleweights. This variable declares weights for selected modules to allow loader sort modules by priority and use already defined settings in each other loaded module. You can define your custom modules and weights there.

Ok, now few examples.

settings/env.py

import os

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

DEBUG = not 'http/ask.helldude.ru/' in os.path.realpath(__file__)

settings/assets.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
import os
import sys

settings = sys.modules['project.settings']

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(settings.BASE_DIR, 'static')
STATICFILES_DIRS = (os.path.join(settings.BASE_DIR, "project/static"),)

STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  'compressor.finders.CompressorFinder',
)

settings_local.py:

import sys

settings = sys.modules['project.settings']

print 'I WAS LOADED KHA KHA KHA'

if settings.DEBUG:
    print 'In debug mode'

You can see amazing 'hack' there :-)

I use already defined settings via sys.modules['project.settings'] (where project is common folder for my project applications).

I hope you like this small lifehack for django settings!

rudude.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import inspect
import pkgutil
import sys

ordering = ('env', 'paths')

modules = [x[1] for x in pkgutil.walk_packages(__path__)]
modules.sort(key=lambda x: x in ordering and ordering.index(x) + 1 or sys.maxint)

print 'Loading project.settings submodules: %s' % (", ".join(modules))

for module_name in modules:
    module = __import__(module_name, globals(), locals(), [])
    for var_name, val in inspect.getmembers(module):
        if var_name.isupper():
            locals().update({var_name: val})

try:
    # noinspection PyUnresolvedReferences
    from ..settings_local import *
except ImportError:
    pass

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.