Question

I'm working on a reusable django app, which I want to make configurable by GLOBAL VARIABLES in settings.py.

I want these to be optional so I've provided default values inside my app in a conf.py module, where I have the following:

GLOBAL_1 = "Def value for global 1"
GLOBAL_2 = "Def value for global 2"
GLOBAL_3 = "Def value for global 3"

try:
    from settings import GLOBAL_1
except:
    pass

try:
    from settings import GLOBAL_2
except:
    pass

...

So defaults can get overwritten by configuration in settings.py should they exist. This method won't scale for too many variables so I was wondering if there's a less verbose way to do this. Maybe I should use eval()?

Thanks a lot!

Was it helpful?

Solution

GLOBAL_1 = getattr(settings, 'GLOBAL_1', 'Def value for global 1')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top