Use Pydantic to enhance your Django application settings.
Project description
Django Base Settings
Use Pydantic to enhance your Django application settings.
Requirements
- Python 3.10 or newer
Installation
To install Django Base Settings, run the following command:
poetry add django-base-settings
Usage
In your Django settings file, define a subclass of DjangoBaseSettings
:
from django_base_settings import DjangoBaseSettings
class MySiteSettings(DjangoBaseSettings):
allowed_hosts: list[str] = ["www.example.com"]
debug: bool = False
default_from_email: str = "webmaster@example.com"
my_site_settings = MySiteSettings()
This is equivalent to:
ALLOWED_HOSTS = ["www.example.com"]
DEBUG = False
DEFAULT_FROM_EMAIL = "webmaster@example.com"
Nested Settings
For more complex configurations, you can define nested settings using Pydantic models:
from django_base_settings import BaseSettings, DjangoBaseSettings
class CacheSettings(BaseSettings):
backend: str = "django.core.cache.backends.redis.RedisCache"
location: str = "redis://127.0.0.1:6379/1"
class MySiteSettings(DjangoBaseSettings):
caches: dict[str, CacheSettings] = {"default": CacheSettings()}
my_site_settings = MySiteSettings()
This configuration is equivalent to:
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
}
}
[!TIP] Import
BaseSettings
andBaseModel
fromdjango_base_settings
for your nested configuration objects instead ofpydantic
andpydantic_settings
. These provide additional useful features such as automatic conversion of lowercase field names to uppercase when creating the Django application settings.
Environment Variables
Fields contained within DjangoBaseSettings and BaseSettings objects can be assigned values or have their default overwritten through environment variables, providing flexibility for different deployment environments.
In this example:
from django_base_settings import DjangoBaseSettings
class MySiteSettings(DjangoBaseSettings):
default_from_email: str = "webmaster@example.com"
my_site_settings = MySiteSettings()
Setting DEFAULT_FROM_EMAIL
as an environment variable will override the default value of default_from_email
:
export DEFAULT_FROM_EMAIL="admin@example.com"
You can also specify a different environment variable name:
from pydantic import Field
from django_base_settings import DjangoBaseSettings
class MySiteSettings(DjangoBaseSettings):
default_from_email: str = Field("webmaster@example.com", env="DEFAULT_EMAIL")
my_site_settings = MySiteSettings()
In this example, setting DEFAULT_EMAIL
as an environment variable will override the default value of default_from_email
:
export DEFAULT_EMAIL="admin@example.com"
Altering Settings
Django does not recommend altering the application settings during runtime. Because of this, all fields defined using DjangoBaseSettings
are frozen and cannot be altered after initilization.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_base_settings-0.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a99cc4f06a2a07faead3768d364f8cc82cf02c59ac4584f5ceac7b5886afbce5 |
|
MD5 | 81b34cae50895c56ae1f2f6fc591bd71 |
|
BLAKE2b-256 | f762c41498bd85f8e1eb8fd88034c3233e8e8ccf9f228e131714fcf9eadd7b58 |
Hashes for django_base_settings-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f4eaadd528730507926d9ecc6361146ecfebd3eba90d71dd478fc7b98c9e188 |
|
MD5 | c5dc6308f483dea5dfc5cfcfc0e3b819 |
|
BLAKE2b-256 | d2c17ec243293439f4fad6b61cd151b17965d398c67d339722999079ba476981 |