0

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) ?

Antwane
  • 20,760
  • 7
  • 51
  • 84
  • There was a feature request for this and they shot it down. It's stupid because everyone needs it. Most people make a settings.py in their app and do FOO=getattr (settings, 'FOO', 'default') – kagronick Feb 11 '17 at 15:20
  • There's no standard way. The simplest is probably, as mentioned by @kagronick, `FOO=getattr (settings, 'FOO', 'default')`. There's also `django-appconf`, which I think is relatively popular, but personally I think the benefits aren't worth the cost of adding an additional third-party module. – Antonis Christofides Feb 11 '17 at 20:47

0 Answers0