tortoise-orm在fastapi中的使用
| |
| """ |
| Tortoise ORM |
| """ |
| |
| |
| from tortoise.contrib.fastapi import register_tortoise |
| from tortoise.contrib.pydantic import pydantic_model_creator |
| from tortoise import models, fields |
| |
| class TortoiseOrm(models.Model): |
| |
| data = fields.CharField(max_length=30) |
| |
| create_date = fields.DatetimeField(auto_now_add=True, null=True) |
| |
| class Meta: |
| table = 'a' |
| |
| |
| |
| Response = pydantic_model_creator(TortoiseOrm, name="OutPut", exclude=("create_date",)) |
| |
| |
| |
| from fastapi import FastAPI |
| app = FastAPI(title="TortoiseORM") |
| |
| register_tortoise( |
| app, |
| db_url="sqlite://basic.sqlite3", |
| modules={"models": ["__main__"]}, |
| add_exception_handlers=True, |
| ) |
| |
| |
| @app.get("/", response_model=Response) |
| async def index(): |
| |
| |
| |
| return await TortoiseOrm.all().order_by("id").first() |
| |
| |
| @app.get("/index") |
| async def index1(): |
| return await TortoiseOrm.all().order_by("id") |
| |
| if __name__ == '__main__': |
| import uvicorn |
| |
| uvicorn.run("__main__:app", reload=True) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-02-11 jQuery如何进行二次查找
2022-02-11 如何查看一个网站有没有使用jQuery
2022-02-11 开发依赖 生产依赖 区别
2022-02-11 JavaScript 数组中的常用方法 数组方法
2022-02-11 ngrok 实现内网穿透
2022-02-11 symbol作为key时的获取与遍历
2022-02-11 如何创建两个相同的symbol