Skip to main content

No project description provided

Project description

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

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

dynaconf_tortoise_loader-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynaconf_tortoise_loader-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file dynaconf_tortoise_loader-0.1.1.tar.gz.

File metadata

File hashes

Hashes for dynaconf_tortoise_loader-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e99dc63f456d75fa9f692388e4d15dda9bf7aabdb680296a67c2d58271d6848c
MD5 cfe4843ae597b0ffba3a7aa6da9c7eb8
BLAKE2b-256 3b80d5a4147e690ca61623790206577bdf7d90e4624918a6b0561b31e2c1da65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dynaconf_tortoise_loader-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7dfd6d4b2ad19c372295c4075f6bfebdb4b749aca707d5b4c4cc8a6281e6ba32
MD5 f1bb869be31e8d361a556472f599dd2b
BLAKE2b-256 f7b831a6723a151c353758d02f6c7348cb0c3b6cce3b6c07074b7c6a78357c30

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page