Pydantic Settings for Django
Project description
pydjantic
Using Pydantic Settings in your Django app.
Introduction
If you are tired of the mess in your Django Settings - I feel your pain:
- Ridiculously long
settings.py
file, with ASCII-art separators from common import *
Python anti-patterntry: <import> except: ImportError
Python anti-patternbase.py
,production.py
,local.py
,domain.py
- bunch of unrelated modules that override each other- django-environ library, that did even worse...
On the other hand we have Pydatic Settings, which is de facto standard for all non-django projects.
If you love Pydatic settings management approach, Pydjantic is a right tool for you.
Pydjantic allows you to define your settings in familiar way - just inherit from BaseSettings
like you would in non-django app.
from typing import List
from pydantic import BaseSettings, Field
from pydantic.fields import Undefined
from pydjantic import to_django
class GeneralSettings(BaseSettings):
SECRET_KEY: str = Field(default=Undefined, env='DJANGO_SECRET_KEY')
DEBUG: bool = Field(default=False, env='DEBUG')
INSTALLED_APPS: List[str] = [
'django.contrib.admin',
'django.contrib.auth',
]
LANGUAGE_CODE: str = 'en-us'
USE_TZ: bool = True
class StaticSettings(BaseSettings):
STATIC_URL: str = '/static/'
STATIC_ROOT: str = 'staticfiles'
class SentrySettings(BaseSettings):
SENTRY_DSN: str = Field(default=Undefined, env='SENTRY_DSN')
class ProjectSettings(GeneralSettings, StaticSettings, SentrySettings):
pass
to_django(ProjectSettings())
You can create as many classes/modules as you want, to achieve perfect settings' management.
Divide your settings by domains, and then create final ProjectSettings
class, that inherits from these domains.
Provide the instance of ProjectSettings
to to_django
function.
That's all, your django settings will work as expected.
Installation
Install using pip install -U pydjantic
or poetry add pydjantic
.
Example
In the /demo
directory you can find a working Django app with pydjantic settings.
Database config
Pydjantic comes with a special helper for managing DB configs - BaseDBConfig
. See example below:
from pydantic import Field, PostgresDsn
from pydjantic import BaseDBConfig
class DatabaseConfig(BaseDBConfig):
default: PostgresDsn = Field(
default="postgres://user:password@hostname:5432/database_name", env="DATABASE_URL"
)
db_settings = DatabaseConfig()
assert db_settings.default == {
'CONN_MAX_AGE': 0,
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'hostname',
'NAME': 'database_name',
'PASSWORD': 'password',
'PORT': 5432,
'USER': 'user',
}
Transformation from dsn to django format is done by dj-database-url.
Project details
Release history Release notifications | RSS feed
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 pydjantic-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22a1b4cd74626f416ffa9c112c3c7c43404c6ea396b8e98636a3ed4c63d065bf |
|
MD5 | cc6ef0e45252ff545600ca7855bfedd3 |
|
BLAKE2b-256 | e238457ca4615169273ba66f4542b67af91a6e4cc77255fa5aa9065fa19da87c |