Skip to main content

Manage Django settings with Pydantic.

Project description

django-pydantic-settings

Use pydantic settings management to simplify configuration of Django settings.

Very much a work in progress, but reads the standard DJANGO_SETTINGS_MODULE environment variable (defaulting to pydantic_settings.Settings) to load a sub-class of pydantic_settings.Settings. All settings (that have been defined in pydantic_settings.Settings) can be overridden with environment variables. A special DatabaseSettings class is used to allow multiple databases to be configured simply with DSNs.

Database configuration

By defining multiple DjangoDsn attributes of the DatabaseSettings class, you can easily configure one or more database connections with environment variables. DSNs are parsed using dj-database-url.

class DatabaseSettings(BaseSettings):
    default: DjangoDsn = Field(env="DATABASE_URL")
    secondary: DjangoDsn = Field(env="SECONDARY_DATABASE_URL")
 DATABASE_URL=sqlite:///foo SECONDARY_DATABASE_URL=sqlite:///bar ./settings_test/manage.py shell
Python 3.9.1 (default, Jan  8 2021, 17:17:43)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
...
>>> pp.pprint(settings.DATABASES)
{   'default': {   'ATOMIC_REQUESTS': False,
                   'AUTOCOMMIT': True,
                   'CONN_MAX_AGE': 0,
                   'ENGINE': 'django.db.backends.sqlite3',
                   'HOST': '',
                   'NAME': 'foo',
                   'OPTIONS': {},
                   'PASSWORD': '',
                   'PORT': '',
                   'TEST': {   'CHARSET': None,
                               'COLLATION': None,
                               'MIGRATE': True,
                               'MIRROR': None,
                               'NAME': None},
                   'TIME_ZONE': None,
                   'USER': ''},
    'secondary': {   'CONN_MAX_AGE': 0,
                     'ENGINE': 'django.db.backends.sqlite3',
                     'HOST': '',
                     'NAME': 'bar',
                     'PASSWORD': '',
                     'PORT': '',
                     'USER': ''}}
>>>

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-pydantic-settings-0.1.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

django_pydantic_settings-0.1.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file django-pydantic-settings-0.1.1.tar.gz.

File metadata

  • Download URL: django-pydantic-settings-0.1.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/2.7.16 Darwin/20.2.0

File hashes

Hashes for django-pydantic-settings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 96bf0d76f350890f0d1afd11cb6a123afb498362fd7248103c7a46f939df9e04
MD5 76cb940592160b41e1fceec5ce97649e
BLAKE2b-256 5033a176741770b759efd7a09ec433796aedda4bbb16147a5d6896a69e846897

See more details on using hashes here.

Provenance

File details

Details for the file django_pydantic_settings-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pydantic_settings-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8cdb8d99197247f4cbc72f7b91bdead82cd906b18d045bdc5ac5ec216ba57da
MD5 4335c81f8033de53c0551b8281ce8086
BLAKE2b-256 c9b15ca1a978ac82cd86b964fa62c35992d8fe5ce0064d276707e29b47dba66d

See more details on using hashes here.

Provenance

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