{"id":22930561,"url":"https://github.com/erhosen-libs/pydjantic","last_synced_at":"2026-04-14T18:29:28.220Z","repository":{"id":45497786,"uuid":"354337114","full_name":"erhosen-libs/pydjantic","owner":"erhosen-libs","description":"Use Pydantic Settings in your Django application.","archived":false,"fork":false,"pushed_at":"2024-05-24T20:18:54.000Z","size":396,"stargazers_count":46,"open_issues_count":4,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-03-01T15:11:12.378Z","etag":null,"topics":["django-settings","pydantic","pydantic-django","python","settings"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erhosen-libs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-03T16:15:08.000Z","updated_at":"2025-12-26T00:54:28.000Z","dependencies_parsed_at":"2024-12-14T10:28:52.187Z","dependency_job_id":"1e920f93-9f28-4ce4-b620-35ce5dde5215","html_url":"https://github.com/erhosen-libs/pydjantic","commit_stats":{"total_commits":27,"total_committers":5,"mean_commits":5.4,"dds":0.5185185185185186,"last_synced_commit":"5547523c8b1833bbd1deb788b968770b05a87ac9"},"previous_names":["erhosen/pydjantic"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/erhosen-libs/pydjantic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erhosen-libs%2Fpydjantic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erhosen-libs%2Fpydjantic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erhosen-libs%2Fpydjantic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erhosen-libs%2Fpydjantic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erhosen-libs","download_url":"https://codeload.github.com/erhosen-libs/pydjantic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erhosen-libs%2Fpydjantic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31810723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T18:05:02.291Z","status":"ssl_error","status_checked_at":"2026-04-14T18:05:01.765Z","response_time":153,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django-settings","pydantic","pydantic-django","python","settings"],"created_at":"2024-12-14T10:28:46.638Z","updated_at":"2026-04-14T18:29:28.199Z","avatar_url":"https://github.com/erhosen-libs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pydjantic\n[![Build Status](https://github.com/ErhoSen/pydjantic/actions/workflows/main.yml/badge.svg)](https://github.com/ErhoSen/pydjantic/actions)\n[![codecov](https://codecov.io/gh/ErhoSen/pydjantic/branch/master/graph/badge.svg?token=BW5A0V3CR3)](https://codecov.io/gh/ErhoSen/pydjantic)\n[![pypi](https://img.shields.io/pypi/v/pydjantic.svg)](https://pypi.org/project/pydjantic/)\n[![versions](https://img.shields.io/pypi/pyversions/pydjantic.svg)](https://github.com/erhosen-libs/pydjantic)\n[![license](https://img.shields.io/github/license/erhosen/pydjantic.svg)](https://github.com/erhosen-libs/pydjantic/blob/master/LICENSE)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nUse Pydantic Settings in your Django application.\n\n![Pydjantc django settings](https://github.com/erhosen-libs/pydjantic/raw/master/images/pydjantic.png \"Pydjantc django settings\")\n\n## Introduction\n\nIf you are tired of the mess in your Django Settings - I feel your pain:\n* Ridiculously long `settings.py` file, with ASCII-art separation\n* `from common import *` Python [anti-pattern](https://www.geeksforgeeks.org/why-import-star-in-python-is-a-bad-idea/)\n* `try: \u003cimport\u003e except: ImportError` Python [anti-pattern](https://stackoverflow.com/questions/14050281/how-to-check-if-a-python-module-exists-without-importing-it)\n* `base.py`, `production.py`, `local.py`, `domain.py` - bunch of unrelated modules that override each other\n* [django-environ](https://github.com/joke2k/django-environ) library, that do even worse...\n\nOn the other hand we have [Pydantic Settings](https://pydantic-docs.helpmanual.io/usage/settings/),\nwhich is de-facto standard for all non-django projects.\n\nIf you love Pydantic settings management approach, **Pydjantic** is a right tool for you.\n\n**Pydjantic** allows you to define your settings in familiar way - just inherit from `BaseSettings`:\n```py\nfrom typing import List\n\nfrom pydantic import BaseSettings, Field\nfrom pydantic.fields import Undefined\nfrom pydjantic import to_django\n\nclass GeneralSettings(BaseSettings):\n    SECRET_KEY: str = Field(default=Undefined, env='DJANGO_SECRET_KEY')\n    DEBUG: bool = Field(default=False, env='DEBUG')\n    INSTALLED_APPS: List[str] = [\n        'django.contrib.admin',\n        'django.contrib.auth',\n    ]\n    LANGUAGE_CODE: str = 'en-us'\n    USE_TZ: bool = True\n\n\nclass StaticSettings(BaseSettings):\n    STATIC_URL: str = '/static/'\n    STATIC_ROOT: str = 'staticfiles'\n\n\nclass SentrySettings(BaseSettings):\n    SENTRY_DSN: str = Field(default=Undefined, env='SENTRY_DSN')\n\n\nclass ProjectSettings(GeneralSettings, StaticSettings, SentrySettings):\n    pass\n\n\nto_django(ProjectSettings())\n```\nYou can create as many classes/modules as you want, to achieve perfect settings' management.\nDivide your settings by domains, and then create final `ProjectSettings` class, that inherits from these domains.\n\nProvide the instance of `ProjectSettings` to `to_django` function.\nThat's all, your django settings will work as expected.\n\n## Installation\n\nInstall using `pip install -U pydjantic` or `poetry add pydjantic`.\n\n## Example\nIn the `/demo` directory you can find a [working Django app](https://github.com/erhosen-libs/pydjantic/tree/master/demo) with [pydjantic settings](https://github.com/erhosen-libs/pydjantic/blob/master/demo/demo/settings.py).\n\n## Database configuration\n\n**Pydjantic** comes with a special helper for managing DB configs - `BaseDBConfig`. See example below:\n```python\nfrom pydantic import Field, PostgresDsn\nfrom pydjantic import BaseDBConfig\n\n\nclass DatabaseConfig(BaseDBConfig):\n    default: PostgresDsn = Field(default=\"postgres://user:password@hostname:5432/dbname\", env=\"DATABASE_URL\")\n\ndb_settings = DatabaseConfig()\nassert db_settings.default == {\n    'CONN_MAX_AGE': 0,\n    'ENGINE': 'django.db.backends.postgresql_psycopg2',\n    'HOST': 'hostname',\n    'NAME': 'dbname',\n    'PASSWORD': 'password',\n    'PORT': 5432,\n    'USER': 'user',\n}\n```\n\nAlso, you can define database configurations directly:\n```python\nfrom pydantic import BaseSettings, Field\n\nclass PostgresDB(BaseSettings):\n    ENGINE: str = 'django.db.backends.postgresql_psycopg2'\n    HOST: str = Field(default='localhost', env='DATABASE_HOST')\n    NAME: str = Field(default='dbname', env='DATABASE_NAME')\n    PASSWORD: str = Field(default='password', env='DATABASE_PASSWORD')\n    PORT: int = Field(default=5432, env='DATABASE_PORT')\n    USER: str = Field(default='user', env='DATABASE_USER')\n    OPTIONS: dict = Field(default={}, env='DATABASE_OPTIONS')\n    CONN_MAX_AGE: int = Field(default=0, env='DATABASE_CONN_MAX_AGE')\n\nclass DatabaseConfig(BaseSettings):\n    default = PostgresDB()\n```\n\nOr mix these approaches:\n```python\nclass DatabaseConfig(BaseDBConfig):\n    default = Field(default=\"postgres://user:password@hostname:5432/dbname\")\n    replica = PostgresDB()\n```\n\nFor more examples see [tests](tests/test_db_config.py).\n\nTransformation from dsn to django format is done by [dj-database-url](https://pypi.org/project/dj-database-url/) library.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferhosen-libs%2Fpydjantic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferhosen-libs%2Fpydjantic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferhosen-libs%2Fpydjantic/lists"}