Decorator
Settings classes
Another way to use dj_settings is to use it as a decorator to create a settings class.
from dj_settings import config_value, settings_class
@settings_class(project_dir=Path("/path/to/project"), filename="config.yml")
class Settings:
user: str = config_value("user", use_env=False, sections=["info"])
email: str = config_value("email", use_env="EMAIL", sections=["info"])
password: str = config_value("PASSWORD", sections=["info", "security"], default="super-secret")
favourite_food: str = "bread"
settings = Settings()
This will create an object that will try to read from the following files for the attributes:
- user
/etc/config.yml, sectioninfo, attributeuser${XDG_CONFIG_HOME}/config.yml, sectioninfo, attributeuser/path/to/project/config.yml, sectioninfo, attributeuser- use
None
- email
/etc/config.yml, sectioninfo, attributeemail${XDG_CONFIG_HOME}/config.yml, sectioninfo, attributeemail/path/to/project/config.yml, sectioninfo, attributeemail- the env var
EMAIL - default to None
- password
/etc/config.yml, sectioninfo, subsectionsecurity, attributePASSWORD${XDG_CONFIG_HOME}/config.yml, sectioninfo, subsectionsecurity, attributePASSWORD/path/to/project/config.yml, sectioninfo, subsectionsecurity, attributePASSWORD- the env var
PASSWORD - default to
super-secret
- favourite_food
- default to
bread
- default to