您好,欢迎访问一九零五行业门户网

Python中的ORM框架Tortoise ORM详解

python是一种不可或缺的编程语言,随着其不断发展,许多功能强大的工具和框架也应运而生。其中,orm(对象关系映射)框架在python开发中具有重要意义。tortoise orm是python中的一种orm框架,它基于异步语法,具有非常高的性能和可扩展性。本文将详细介绍tortoise orm的特点和基本用法。
什么是orm框架?orm框架是一种将面向对象编程语言中的数据对象映射至关系数据库的技术。orm通过数据对象模型来提升开发效率,使得开发者可以使用面向对象的编程接口操作关系数据库,而不需要手写sql语句,通过面向对象的方式进行数据处理。
tortoise orm的特点tortoise orm是基于python 3.6+ language的异步orm库。下面是tortoise orm的主要特点:
(1) 效率高:tortoise orm采用异步语法,能够支持高度并发,性能非常出色。
(2) 易用性强:tortoise orm使用简单,支持类似于django orm的语法,提供了一整套orm模型(meta类)。
(3) 易扩展:为了更好的扩展性,tortoise orm将model编写从数据库分离出来,这样我们可以更方便的进行代码重构。
(4) 插件丰富:tortoise orm支持许多第三方插件,如异步elasticsearch、postgresql、aiopg、pgamdin、asyncssh、apscheduler、aiocache等等。
如何使用tortoise orm首先,我们先需要一个mysql数据库和pymysql驱动程序。如果您还没有安装mysql数据库和pymysql驱动程序,可以使用以下命令进行安装:
pip install pymysql
如果您的mysql服务器没有打开ssl,请安装标志 -use-optionssl = true,以使用ssh加密连接:
pip install pymysql[ssl]
接下来,我们要安装tortoise orm及其依赖项:
pip install tortoise-orm
为了使用tortoise orm,我们需要创建一个tortoise orm对象。在这个对象中,我们可以指定连接数据库的url,用户名,密码和数据库名。例如:
from tortoise import tortoisetortoise_orm = { "connections": {"default": "mysql://user:password@127.0.0.1:3306/test_db"}, "apps": { "models": { "models": ["__main__"], "default_connection": "default", }, },}async def init_orm(): await tortoise.init(tortoise_orm) await tortoise.generate_schemas()asyncio.run(init_orm())
这里,我们定义了一个tortoise_orm字典,其中指定了数据库连接的url和应用程序列表(在这种情况下,我们只使用__main__模块)。然后,我们创建了一个init_orm函数,在其中初始化tortoise对象,并生成数据库架构。
接下来,我们可以定义一个数据库模型(model),它是一个python类,并且需要继承tortoise中所提供的model。例如:
from tortoise import fields, modelsclass user(models.model): id = fields.intfield(pk=true) username = fields.charfield(50, unique=true) password = fields.charfield(128) created_at = fields.datetimefield(auto_now_add=true) updated_at = fields.datetimefield(auto_now=true)
在这里,我们定义了一个user模型,它有一个id字段,一个用户名字段,一个密码字段,以及两个时间戳字段(创建时间和更新时间)。需要注意的是,我们的id 字段需要使用pk=true来指定主键。
现在,我们可以使用orm模型访问数据库。例如,如果我们要插入一个新的user到数据库中,我们可以使用以下代码:
async def create_user(username: str, password: str) -> user: user = user(username=username, password=password) await user.save() return user
在这里,我们先创建了一个user对象,然后调用save()方法将其保存到数据库中,最后返回user对象。我们还可以使用以下代码从数据库中检索user:
async def get_user_by_username(username: str) -> union[none, user]: return await user.filter(username=username).first()
在这里,我们可以使用user类的filter方法按用户名查找用户,然后调用first()方法来返回第一个匹配结果。
tortoise orm插件作为一个开放和灵活的orm框架,tortoise orm支持许多插件。下面是一些插件的简介:
(1) tortoise-orm[elastic]:支持异步elasticsearch。
(2) tortoise-orm[asyncpg]:postgresql数据库支持。
(3) tortoise-orm[aiopg]:aiopg数据库支持。
(4) tortoise-orm[admin]:可视化界面和管理工具。
(5) tortoise-orm[sqlite]:sqlite数据库支持。
(6) tortoise-orm[aiofiles]:文件存储支持。
(7) tortoise-orm[aioredis]:redis数据库支持。
(8) tortoise-orm[asyncssh]:ssh客户端和服务端支持。
(9) tortoise-orm[aiohttp]:使用aiohttp库的web框架支持。
这些插件可以根据你的业务需求随意挑选。你可以选择使用tortoise orm,以及适合你的业务需求的插件来提高你的开发效率。
总结tortoise orm是一种性能出色且易用的orm库,功能强大,具有良好的扩展性和插件机制。它采用异步语法,可以支持高度并发。同时,tortoise orm还提供了类似于django orm一样的语法,使用简单。在python应用程序中,它可以大大提高开发效率,并且可以帮助应用程序与数据库进行更高效的交互。
以上就是python中的orm框架tortoise orm详解的详细内容。
其它类似信息

推荐信息