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 pydantic import Field
from pydantic_settings import BaseSettings
from django_base_settings import DjangoBaseSettings
class CacheSettings(BaseSettings):
backend: str = Field("django.core.cache.backends.redis.RedisCache", alias="BACKEND")
location: str = Field("redis://127.0.0.1:6379/1", alias="LOCATION")
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",
}
}
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()
You can configure the value of default_from_email by creating an environment variable, which will overwrite the default value:
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
.
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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f037af8331a1f42e9774045d3ffc85f6da8db1159c8eb85cd47bbfd6cbb6f6c |
|
MD5 | 5c2925889b8c5291aed3d1c9752b18e8 |
|
BLAKE2b-256 | 8e04d423b6b1d24ec9fd0b5308c7bf1e200e2291078a427b240c64080ab6395f |
Hashes for django_base_settings-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 023f0ddc82b4964362cad53f9050d94da3594df97858134c6135d36f5ca2409d |
|
MD5 | 6b1ae81d8c3b2dc14062447b72a13bb9 |
|
BLAKE2b-256 | e632e2a2d0521c5433b3c124b085c3b338987c894c78345d4efc7931f4a152da |