Created
November 12, 2015 13:57
-
-
Save gcetusic/969c203b60664bb71e9a to your computer and use it in GitHub Desktop.
Override Django project global settings on application load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def inject_app_defaults(application): | |
| """Inject an application's default settings""" | |
| try: | |
| __import__('%s.settings' % application) | |
| import sys | |
| # Import our defaults, project defaults, and project settings | |
| _app_settings = sys.modules['%s.settings' % application] | |
| _def_settings = sys.modules['django.conf.global_settings'] | |
| _settings = sys.modules['django.conf'].settings | |
| # Add the values from the application.settings module | |
| for _k in dir(_app_settings): | |
| if _k.isupper(): | |
| # Add the value to the default settings module | |
| setattr(_def_settings, _k, getattr(_app_settings, _k)) | |
| # Add the value to the settings, if not already present | |
| if not hasattr(_settings, _k): | |
| setattr(_settings, _k, getattr(_app_settings, _k)) | |
| except ImportError: | |
| # Silently skip failing settings modules | |
| pass | |
| inject_app_defaults(__name__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment