Skip to main content

Django Base Settings

Latest Release Python Django Version MIT License

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",
    }
}

[!NOTE] Import BaseModel/BaseSettings from django_base_settings for your nested configuration objects instead of pydantic/pydantic_settings. These provide additional features, which are necessary to generate a valid Django configuration.

Environment Variables

Fields contained within DjangoBaseSettings and BaseSettings objects can be a assigned value or have their default value 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"

Pydantic Fields

You can use fields from Pydantic to further enhance your settings and improve the validation of the configuration. For example, for setting up CacheSettings, you can define the location as RedisDsn instead of str:

from pydantic import RedisDsn

from django_base_settings import BaseSettings, DjangoBaseSettings

class CacheSettings(BaseSettings):
    backend: str = "django.core.cache.backends.redis.RedisCache"
    location: RedisDsn = "redis://127.0.0.1:6379/1"

class MySiteSettings(DjangoBaseSettings):
    caches: dict[str, CacheSettings] = {"default": CacheSettings()}

my_site_settings = MySiteSettings()

The above code ensures thelocation field adheres to the RedisDsn format, providing an extra layer of validation on your settings.

[!TIP] For more detailed information on DSN types and their usage, refer to the pydantic documentation on network types.

Altering Settings

Django does not recommend altering the application settings during runtime. To align with this best practice, all fields defined using DjangoBaseSettings are immutable and cannot be modified after initialization.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for django-base-settings 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-base-settings 0.5.0
File Size Uploaded
django_base_settings-0.5.0.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-base-settings 0.5.0
File Interpreter ABI Platform
django_base_settings-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / django_base_settings-0.5.0.tar.gz

Download URL django_base_settings-0.5.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
dfae92e25a7b4d3e3a13d97299f973ed077aa54ccee6fac3cf6676a0d3928f21
BLAKE2b-256 checksum
How to use checksums
9c33d109f7bdea4c733845382632d18ee30a862961d974b1e52516278ffa109d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.10 Linux/6.6.37-1-lts

Release files / django_base_settings-0.5.0-py3-none-any.whl

Download URL django_base_settings-0.5.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b64c6a93f67e04da89201ccd8bb7036123aca88f1566b9b060bca9c1bd69691
BLAKE2b-256 checksum
How to use checksums
9d526e4f187111b13e092448f8579e370b0cdb20526207d9262a094c4e91be56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.10 Linux/6.6.37-1-lts

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page