Skip to main content

Use Pydantic to enhance your Django application settings.

Project description

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.

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

django_base_settings-0.5.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

django_base_settings-0.5.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file django_base_settings-0.5.0.tar.gz.

File metadata

  • Download URL: django_base_settings-0.5.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.10 Linux/6.6.37-1-lts

File hashes

Hashes for django_base_settings-0.5.0.tar.gz
Algorithm Hash digest
SHA256 dfae92e25a7b4d3e3a13d97299f973ed077aa54ccee6fac3cf6676a0d3928f21
MD5 50be35a68b41fc9a3b4ccc6efd1612ca
BLAKE2b-256 9c33d109f7bdea4c733845382632d18ee30a862961d974b1e52516278ffa109d

See more details on using hashes here.

File details

Details for the file django_base_settings-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_base_settings-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b64c6a93f67e04da89201ccd8bb7036123aca88f1566b9b060bca9c1bd69691
MD5 0b56a633f82675c2fe507e10daba2a96
BLAKE2b-256 9d526e4f187111b13e092448f8579e370b0cdb20526207d9262a094c4e91be56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page