A simple Django package setting loader that utilizes dataclasses for type hinting and type checking
Project description
Django DataclassConf
A simple Django package setting loader that utilizes dataclasses for type hinting and type checking.
Bring modern Python typing, robust validation, and full IDE autocomplete to your Django configurations.
django-dataclassconf allows you to bind your Django settings cleanly to structured standard Python dataclasses. Powered by dacite, it automatically catches dynamic setting updates while ensuring your configuration layer stays type-safe and isolated.
But Why?
-
Fail-Fast Validation: Catch bad configuration types or missing values instantly during server startup or container deployment rather than hitting silent runtime crashes mid-request.
-
Full IDE Autocomplete: Say goodbye to blind
getattr(settings, "MY_SETTING")calls. Enjoy full hovering type definitions and autocompletion in VS Code, PyCharm, and MyPy. -
Dual Format Normalization: Merges flat environment styles (
PREFIX_TIMEOUT = 30) and structured dictionary blocks (PREFIX = {"TIMEOUT": 30}) into a single unified object seamlessly. -
Test-Safe Isolation: Fully supports Django's test suite cycles. When settings are overridden dynamically in unit tests, your dataclasses mutate cleanly in-place and revert automatically.
Usage
1. Define Your Configuration Dataclass
Create a file named config.py (or any name you prefer tbh) within your application or package.
Inherit from BaseConfig and define your variables.
from dataclasses import dataclass, field
from django_dataclassconf.config import BaseConfig, config_loader
@dataclass
class MyPackageConfig(BaseConfig):
DOCUMENTS_ROOT_PATH: str = '/the/default/path/'
MAX_FILE_SIZE: int = 10000
INDEXER_CLASS: str = 'myapp.utils.DocumentIndexer'
@property
def _prefix(self) -> str:
"""
Define the config's prefix here, return blank string if it doesn't
have a prefix such as when we write a configuration dataclass that
will hold the `DEBUG` setting
"""
return 'MY_PACKAGE'
package_config = MyPackageConfig()
2. Subscribe to the Configuration Loader
For your dataclass to grab configuration data from Django's settings.py on startup and capture updates during tests, subscribe your instance into config_loader inside your app's initialization hook:
# my_app/apps.py
class MyAppConfig(AppConfig):
name = 'my_app'
def ready(self):
from django_dataclassconf.config import config_loader
from .config import package_config
config_loader.subscribe(package_config)
3. Access Your Settings Anywhere
Core Practice would be to import your configuration instance directly instead of using the global django.conf.settings object to harness the full type safety and IDE autocomplete.
# my_app/views.py
from django.http import HttpResponse
from my_app.config import package_config
def my_view(request):
...
# Your IDE now natively autocompletes these fields
if file_size > package_config.MAX_FILE_SIZE:
return HttpResponse(
{'detail': 'File size has exceeded the maximum size limit!'},
status = 400
)
Extras
Your Configuration Dataclass can also be a nested dataclasses, you only need to inherit BaseConfig to the root configuration dataclass.
from dataclasses import dataclass, field
from django_dataclassconf.config import BaseConfig, config_loader
@dataclass
class DocumentPreview:
preview_page_count: int = 10
strip_cover_page: bool = False
@dataclass
class MyPackageConfig(BaseConfig):
DOCUMENTS_ROOT_PATH: str = '/the/default/path/'
PREVIEW: DocumentPreview = field(default_factory=DocumentPreview)
@property
def _prefix(self) -> str:
return 'MY_PACKAGE'
configuration = MyPackageConfig()
config_loader.subscribe(configuration)
And it will look like this in settings.py
MY_PACKAGE = {
'DOCUMENTS_ROOT_PATH': '/custom/path/',
'PREVIEW': {
'preview_page_count': 12,
'strip_cover_page': True
},
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_dataclassconf-0.1.0.tar.gz.
File metadata
- Download URL: django_dataclassconf-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2d13762ff39daceff31b61a9c4d88144ae217e4d31634ad8f812697f3ec71fb
|
|
| MD5 |
6ab881a75c99534b09b80aa670af6aec
|
|
| BLAKE2b-256 |
0006514a5d434fff2535a64038cfc43c2abb177e2e3956a6b74546893e625ea9
|
File details
Details for the file django_dataclassconf-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_dataclassconf-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5cabe7e24da201d86192b96436bc41248b6027152f21edb963ce1a3fe42f1db
|
|
| MD5 |
61096f417243d2abd614e1189c8e65b4
|
|
| BLAKE2b-256 |
44220c1f5c677bb071a3885f689e430cca3537120cc2fb01068101c79ee7cd7b
|