异步PeeWee

aiopeewee的Python项目详细描述


Build StatusGitential Active ContributorsGitential Coding HoursGitential Efficiency

空气压缩机

torpeewee之后建模的peewee的异步接口

实现的数据库适配器:

  • [X]aiomysql
  • []AIOPG
  • []sqlite

目前,peewee已经移植了125个测试用例,并不是全部,而是不断增加。

也支持简单的原子操作(事务),但现在已经过了很好的测试。

安装

pip install aiopeewee

使用量

fromaiopeeweeimportAioModel,AioMySQLDatabasefrompeeweeimportCharField,TextField,DateTimeFieldfrompeeweeimportForeignKeyField,PrimaryKeyFielddb=AioMySQLDatabase('test',host='127.0.0.1',port=3306,user='root',password='')classUser(AioModel):username=CharField()classMeta:database=dbclassBlog(AioModel):user=ForeignKeyField(User)title=CharField(max_length=25)content=TextField(default='')pub_date=DateTimeField(null=True)pk=PrimaryKeyField()classMeta:database=db# create connection poolawaitdb.connect(loop)# countawaitUser.select().count()# async iteration on select queryasyncforuserinUser.select():print(user)# fetch all records as a list from a query in one passusers=awaitUser.select()# insertuser=awaitUser.create(username='kszucs')# modifyuser.username='krisztian'awaituser.save()# async iteration on blog set[b.titleasyncforbinuser.blog_set.order_by(Blog.title)]# close connection poolawaitdb.close()# see more in the tests

许多

注意,必须使用aiomanytomanyfield而不是manytomy。

fromaiopeeweeimportAioManyToManyFieldclassUser(AioModel):username=CharField(unique=True)classMeta:database=dbclassNote(AioModel):text=TextField()users=AioManyToManyField(User)classMeta:database=dbNoteUserThrough=Note.users.get_through_model()asyncforuserinnote.users:# do something with the users

目前,我所知道的实例关系的immidiate设置的唯一限制必须替换为方法调用:

# original, which is not supportedcharlie.notes=[n2,n3]# use insteadawaitcharlie.notes.set([n2,n3])

序列化

转换为dict需要异步版本的model-to-dict

fromaiopeeweeimportmodel_to_dictserialized=awaitmodel_to_dict(user)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法在spring boot应用程序中启用本机内存跟踪   jakarta ee在Java Web项目上的多窗口   日期将Java时间戳转换为MySQL时间戳,反之亦然   java如何实现异步任务连接到服务器并解析JSON   java为什么我得到索引越界异常?   我们如何在java中以大写字母和小写字母存储同名文件   jni/java:有效不可变本机对象的线程安全发布/共享   Java将文本写入远程文件   int最小硬币算法   java如何设置/获取我在类Vehicle的主方法中创建的类Car的“ford”实例的名称?   java使用计时器在队列已满时重新调度使用者   java从字符串的末尾提取一个子字符串,直到遇到第一个空格为止?   java在SimpleApplication之外正确初始化物理状态