Providing more generic settings module

Can we create a more generic django.conf.LazySettings object? I am creating some settings or configuration file (not really related to Django project) but inside my django codebase. But similar to django.conf.settings there will be some default values and some overridden values. Now this part can be achieved in many ways, but can we provide a native way through Django? Below is my expected behavior:

# app_default_settings.py
RUNTIME = "myapp.runtimes.DefaultRuntime"
LOAD_TARGET = "myapp.targets.DBTarget"

# app_settings.py
RUNTIME = "<overridden value>"
LOAD_TARGET = "<overridden value>"

# myapp/conf.py (maybe)
app_settings = GenericSettings(settings_mod="myapp.app_settings",
                               default_mod="myapp.app_default_settings")

# target usage
from myapp.conf import settings

use(settings.RUNTIME)

Welcome @atharva !

A settings file is just a Python module.

It can import values from other settings files.

This means that you can import a “base” or “default” settings file and only override the differences.

e.g. In settings.py:

import app_default_settings
import app_settings

... and other variables as necessary ...

(If this is not clear or does not appear to address the issue, please post a more complete description of what you’re trying to do.)

Thanks for the response Ken!
But Django’s settings internally uses LazySettings, which also internally uses Settings class. Now a case may come where I don’t want to import django.conf.settings object and use custom settings as I mentioned in my example. For some reasons, Settings class uses global_settings module implicitly, that contains all default settings. Can we modify this part to make it extensible for other users?

I’m sorry, I’m not seeing where the current behavior is in any way a problem or limitation.

You can always override a default setting.

Please provide a real example, with a description of the use case and the code necessary to create the problem, of where the current Settings class and behavior creates a problem or limitation.

In the absence of a real and identified issue, apply the YAGNI principle

1 Like