TortoiseCLI

本文档描述了如何使用 tortoise-cli,它是一个用于 tortoise-orm 的 cli 工具,建立在 click 和 ptpython 之上。

你可以查看 https://github.com/tortoise/tortoise-cli 了解更多详细信息。

快速入门

> tortoise-cli -h                                                                                                                                                                 23:59:38
Usage: tortoise-cli [OPTIONS] COMMAND [ARGS]...

Options:
  -V, --version      Show the version and exit.
  -c, --config TEXT  TortoiseORM config dictionary path, like settings.TORTOISE_ORM
  -h, --help         Show this message and exit.

Commands:
  shell  Start an interactive shell.

用法

首先,你需要创建一个 TortoiseORM 配置对象,假设在 settings.py 中。

TORTOISE_ORM = {
    "connections": {
        "default": "sqlite://:memory:",
    },
    "apps": {
        "models": {"models": ["examples.models"], "default_connection": "default"},
    },
}

交互式外壳

然后,你可以为 TortoiseORM 启动一个交互式外壳。

tortoise-cli -c settings.TORTOISE_ORM shell

或者,你可以通过设置环境变量来设置配置。

export TORTOISE_ORM=settings.TORTOISE_ORM

然后只需运行

tortoise-cli shell