Skip to main content

Dynaconf Tortoise ORM Loader

PyPI - License PyPI - Wheel PyPI - Version PyPI - Python Version

release develop

Implementing Dynaconf settings storage in a database using the Tortoise ORM framework.

Installation

pip install dynaconf-tortoise-loader

By default, Tortoise ORM comes with a sqlite database driver.

If you require a different database driver, please refer to the Tortoise ORM documentation.

Usage

  1. Create a config.py file in your project

    import logging
    from pathlib import Path
    from typing import Any
    
    from dynaconf import Dynaconf, ValidationError
    from dynaconf.utils.boxing import DynaBox
    from dynaconf.utils.functional import empty
    
    from dynaconf_tortoise_loader import loader
    
    _BASE_DIR = Path.cwd()
    _DB_PATH = _BASE_DIR / "data" / "settings.sqlite3"
    
    
    class Config(Dynaconf):
        def __init__(self, *args: Any, **kwargs: Any) -> None:
            super().__init__(*args, **kwargs)
            self._initialized = True
    
        def __setattr__(self, name: str, value: Any) -> None:
            if name not in [
                "_wrapped",
                "_kwargs",
                "_warn_dynaconf_global_settings",
            ]:
                if self._wrapped is empty:
                    self._setup()
    
            super().__setattr__(name, value)
    
            if hasattr(self, "_initialized") and self._initialized:
                try:
                    data = DynaBox({name: value}, box_settings={}).to_dict()
                    loader.write(self, data)
                except Exception as e:
                    logging.error(e)
    
    
    settings = Config(
        # Define the environments to use
        # environments=True,
        envvar_prefix="MYAPP",
        settings_files=[
            _BASE_DIR / "settings.toml",
            _BASE_DIR / ".secrets.toml",
        ],
        LOADERS_FOR_DYNACONF=[
            "dynaconf_tortoise_loader.loader",  # require custom loader
            "dynaconf.loaders.env_loader",  # require dotenv loader
        ],
        TORTOISE_ENABLED_FOR_DYNACONF=True,
        TORTOISE_URL_FOR_DYNACONF=f"sqlite://{_DB_PATH}",
    )
    
    try:
        settings.validators.validate_all()
    except ValidationError as e:
        logging.error(e.message)
    

    The TORTOISE_URL_FOR_DYNACONFvalue must contain the database connection string. See the Tortoise ORM documentation for more details.

    TORTOISE_URL_FOR_DYNACONF does not need to be declared directly in the code, but can be declared in the settings files listed in settings_files or set via an environment variable.

    For more information, please refer to the Dynaconf documentation.

  2. Use the settings object in your code

    form .config import settings
    
    # set and store settings
    settings.host = "localhost"
    settings.port = 8022
    
    # get settings
    print(settings.get("host"))
    print(settings.get("port"))
    

    See more example

Metadata

Release files for dynaconf-tortoise-loader 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dynaconf-tortoise-loader 0.1.1
File Size Uploaded
dynaconf_tortoise_loader-0.1.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dynaconf-tortoise-loader 0.1.1
File Interpreter ABI Platform
dynaconf_tortoise_loader-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / dynaconf_tortoise_loader-0.1.1.tar.gz

Download URL dynaconf_tortoise_loader-0.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e99dc63f456d75fa9f692388e4d15dda9bf7aabdb680296a67c2d58271d6848c
BLAKE2b-256 checksum
How to use checksums
3b80d5a4147e690ca61623790206577bdf7d90e4624918a6b0561b31e2c1da65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / dynaconf_tortoise_loader-0.1.1-py3-none-any.whl

Download URL dynaconf_tortoise_loader-0.1.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7dfd6d4b2ad19c372295c4075f6bfebdb4b749aca707d5b4c4cc8a6281e6ba32
BLAKE2b-256 checksum
How to use checksums
f7b831a6723a151c353758d02f6c7348cb0c3b6cce3b6c07074b7c6a78357c30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page