I am working on a Django library, something released on Pypi as a plugable component. Some settings in this plugin should be customized by users, simply by setting values in settings.py
. Currently I use this logic:
# config.py
import django.conf
class DefaultValues(object):
SETTING_1 = []
SETTING_2 = 42
class SettingsWrapper(object):
def __getattr__(self, item):
result = getattr(django.conf.settings, item, None)
if result:
return result
return getattr(DefaultValues, item)
settings = SettingsWrapper()
And in my library, when I need to get the setting value, I do:
from mylib.config import settings
[...]
settings.SETTING_1
It works fine, but I feel I reinvent the wheel. The goal here is to get value first from django.conf.settings
module (user settings), and if the value is not defined, retrieve it from DefaultValues
class.
Recently, digging into Django code, I discovered the django.conf
module, with the classes LazySettings
, BaseSettings
and Settings
. In particular, the doc for BaseSettings
say:
Common logic for settings whether set by a module or by the user.
My question is: can I transform my module conf
to use Django classes and perform the same work (maybe better) ?