Tortoise-ORM BlackSheep 集成

我们有一个轻量级集成工具 tortoise.contrib.blacksheep,它有一个函数 register_tortoise,它在启动时设置 Tortoise-ORM,并在关闭时清理。

BlackSheep 是一个异步 Web 框架,用于使用 Python 构建基于事件的 Web 应用程序。

请参阅 BlackSheep 示例 & 查看 Pydantic 序列化 教程。

参考

tortoise.contrib.blacksheep.register_tortoise(app, config=None, config_file=None, db_url=None, modules=None, generate_schemas=False, add_exception_handlers=False)[source]

注册 startupshutdown 事件,用于在 BlackSheep 应用程序中设置和关闭 Tortoise-ORM。

你可以仅使用 configconfig_file(db_url, modules) 中的一个进行配置。

参数:
app

BlackSheep 应用程序。

config=None

包含配置的字典

示例

{
    'connections': {
        # Dict format for connection
        'default': {
            'engine': 'tortoise.backends.asyncpg',
            'credentials': {
                'host': 'localhost',
                'port': '5432',
                'user': 'tortoise',
                'password': 'qwerty123',
                'database': 'test',
            }
        },
        # Using a DB_URL string
        'default': 'postgres://postgres:qwerty123@localhost:5432/events'
    },
    'apps': {
        'models': {
            'models': ['__main__'],
            # If no default_connection specified, defaults to 'default'
            'default_connection': 'default',
        }
    }
}

config_file=None

指向 .json 或 .yml(如果安装了 PyYAML)文件的路径,其中包含与上述格式相同的配置。

db_url=None

使用 DB_URL 字符串。请参阅 DB_URL

modules=None

字典 key: [list_of_modules],其中定义了应为模型发现的“应用程序”和模块。

generate_schemas=False

True 表示立即生成架构。仅对开发环境或 SQLite :memory: 数据库有用

add_exception_handlers=False

True 表示为 DoesNotExistIntegrityError 添加一些自动异常处理程序。对于生产系统,不建议这样做,因为它可能会泄露数据。

引发:

ConfigurationError – 对于任何配置错误

返回类型: