ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

fastapi里面tortoise-orm的用法

fastapi里面tortoise-orm的用法

1.文档地址

https://blog.csdn.net/weixin_53909748/article/details/131747197

2.创建实体类,model/models.py

from tortoise.models import Model from tortoise import fields class Teacher(Model): id = fields.IntField(pk=True) name = fields.CharField(max_length=255, description='姓名') tno = fields.IntField(description='账号') pwd = fields.CharField(max_length=255, description='密码')

3.安装

PostgreSQL 用户:pip install tortoise-orm[asyncpg] MySQL 用户:pip install tortoise-orm[aiomysql] SQLite 用户:pip install tortoise-orm (默认自带 aiosqlite)

4.TORTOISE_ORM配置文件编写

TORTOISE_ORM = { 'connections': { 'default': { # 'engine': 'tortoise.backends.asyncpg', PostgreSQL 'engine': 'tortoise.backends.mysql', # MySQL or Mariadb 'credentials': { 'host': '127.0.0.1', 'port': '3306', 'user': 'root', 'password': 'yuan0316', 'database': 'fastapi', 'min
返回列表