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

[!TIP] Import BaseSettings and BaseModel from django_base_settings for your nested configuration objects instead of pydantic and pydantic_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

django_base_settings-0.4.1.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

django_base_settings-0.4.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

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