python轻量级ORM---peewee之API

1.classmethods

such
as select/update/insert/delete queries。

# Example:

class User(Model):
    username = CharField()
    join_date = DateTimeField()
    is_admin = BooleanField()

u = User(username='charlie', is_admin=True)
<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;">tw = (Tweet.select(Tweet, User).join(User).order_by(Tweet.created_date.desc()))</span>
<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="color: rgb(64, 64, 64); font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(252, 252, 252);">Example showing an atomic update:</span>
</span>
<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="color: rgb(64, 64, 64); font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(252, 252, 252);"></span></span><pre name="code" class="python">q = PageView.update(count=PageView.count + 1).where(PageView.url == url)
q.execute()  # execute the query, updating the database.

Example
showing creation of a new user:


<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="color: rgb(64, 64, 64); font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(252, 252, 252);"></span></span><pre name="code" class="python">q = User.insert(username='admin', active=True, registration_expired=False)
q.execute()  # perform the insert.

You can also use Field objects as the keys:


<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="color: rgb(64, 64, 64); font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(252, 252, 252);"></span></span><pre name="code" class="python">User.insert(**{User.username: 'admin'}).execute()

高级用法:insert_many(rows):


Example of inserting multiple Users:
<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; line-height: 24px; background-color: rgb(252, 252, 252);"><strong><span style="font-size:14px;color:#404040;"></span></strong></span></span><pre name="code" class="python">usernames = ['charlie', 'huey', 'peewee', 'mickey']
row_dicts = [{'username': username} for username in usernames]

# Insert 4 new rows.
User.insert_many(row_dicts).execute()

Because the rows parameter can be an arbitrary iterable, you can also use a generator:


<span style="background-color: rgb(255, 255, 255); font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; line-height: 24px; background-color: rgb(252, 252, 252);"><strong><span style="font-size:14px;color:#404040;"></span></strong></span></span><pre name="code" class="python">def get_usernames():
    for username in ['charlie', 'huey', 'peewee']:
        yield {'username': username}
User.insert_many(get_usernames()).execute()

Example showing the deletion of all inactive users:


<span style="font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; line-height: 1.5;"><span style="font-family: Lato, proxima-nova, 'Helvetica Neue', Arial, sans-serif; line-height: 24px;"><strong><span style="color:#404040;"><span style="font-size:14px;"></span></span></strong></span></span><pre name="code" class="python" style="background-color: rgb(252, 252, 252);">q = User.delete().where(User.active == False)
q.execute()  # remove the rows

Warning:This
method performs a delete on the entire table. To delete a single instance, see Model.delete_instance().

待续。。。


<pre style="box-sizing: border-box; font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier, monospace; margin-top: 0px; margin-bottom: 0px; padding: 12px; line-height: 1.5; overflow: auto; color: rgb(64, 64, 64); background-color: rgb(255, 255, 255);">



python轻量级ORM---peewee之API

时间: 2024-08-02 15:19:13

python轻量级ORM---peewee之API的相关文章

python轻量级ORM---peewee

peewee是一个轻量级的ORM.用的是大名鼎鼎的sqlalchemy内核,采用纯python编写,显得十分轻便.为了后续方便查看,在这里简单记录下~~ peewee不仅轻量级,还提供了多种数据库的访问,如SqliteDatabase(file or memory).MYSQLDatabase.PostgresqlDatabase: 接下来就从API上路吧~~~ 1. class  fn---To express functions in peewee, use the fn object. F

Android轻量级ORM框架ActiveAndroid入门教程(转)

注:没有找到出处,如有侵犯,请告知 开始ActiveAndroid神奇之旅: 在AndroidManifest.xml中我们需要添加这两个 AA_DB_NAME (数据库名称,这个name不能改,但是是可选的,如果不写的话 是默认的"Application.db"这个值) AA_DB_VERSION (数据库版本号,同样是可选的 – 默认为1) <manifest ...> <application android:name="com.activeandro

Python MySQL ORM QuickORM hacking

# coding: utf-8 # # Python MySQL ORM QuickORM hacking # 说明: # 以前仅仅是知道有ORM的存在,但是对ORM这个东西内部工作原理不是很清楚, # 这次正好需要用到,于是解读一个相对来说很简单的Python2 ORM的例子. # # 参考源码: # A simple ORM provides elegant API for Python-MySQL operation # https://github.com/2shou/QuickORM

.NET轻量级ORM组件Dapper葵花宝典

一.摘要 为什么取名叫<葵花宝典>? 从行走江湖的世界角度来讲您可以理解为一本"武功秘籍",站在我们IT编程的世界角度应该叫"开发宝典". 如果您在工作中主要接触的是操作MySQL数据库,但您又想学习和了解.NET轻量级ORM框架Dapper,那么就请跟着阿笨一起学习本次的分享课<.NET轻量级ORM框架Dapper葵花宝典>.Let's Go,Do It ,Dapper For MySQL! 废话不多说,直接上干货,我们不生产干货,我们只是

c# 轻量级ORM框架 实现(一)

发布一个自己写的一个轻量级ORM框架,本框架设计期初基于三层架构.所以从命名上来看,了解三层的朋友会很好理解. 设计该框架的目的:不想重复的写增删改查,把精力放到功能实现上. 发布改框架的原因:希望给初学者一个参考,希望能给予好的建议,给自己一个展示机会. 在我开始之前,先说明一下,我对"软件工程学"概念东西几乎不通,最高文化程度:初二,所以不喜勿喷. 开始我的orm设计最底层 最底层的是一个DalBase,它是一个抽象的,实现了增删改查的基本操作. 它既然是一个抽象的,那么它的内部就

.NET轻量级ORM组件Dapper修炼手册

一.摘要 1.1.为什么叫本次的分享课叫<修炼手册>? 阿笨希望本次的分享课中涉及覆盖的一些小技巧.小技能给您带来一些帮助.希望您在日后工作中把它作为一本实际技能手册进行储备,以备不时之需,一旦当手头遇到与Dapper修炼手册中相似用法的地方和场景,可以直接拿来进行翻阅并灵活的运用到项目中.最后阿笨建议您可以根据自己在工作中碰到的不同的使用场景,不断的完善此本修炼手册. 废话不多说,直接上干货,我们不生产干货,我们只是干货的搬运工. 四.涉及覆盖的知识点 1.C# Linq To Xml技术.

轻量级ORM框架初探-Dapper与PetaPoco的基本使用

一.EntityFramework EF是传统的ORM框架,也是一个比较重量级的ORM框架.这里仍然使用EF的原因在于为了突出轻量级ORM框架的性能,所谓有对比才有更优的选择. 1.1 准备一张数据库表 (1)For MSSQL CREATE TABLE [dbo].[Posts] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [CategoryId] INT NOT NULL, [Slug] VARCHAR(120) NOT NULL, [Title] N

c# 轻量级 ORM 框架 之 DBHelper 实现 (三)

周末了比较清闲,把自己的orm框架整理了下,开源了. 已经做出来的东西通常感觉有些简单,一些新手或许听到"框架"一类的词觉得有些"高深",简单来说orm就是把ado的封装. 在介绍这个框架的第一篇博文,已经把DalBase介绍了一下设计思路,本篇的DBHelper对象也是给dalBase来用的,可以说框架的所有定义对象都是为了它. 这里起名叫DBHelper,因为我也是从写SQLHelper开始的,DBHelper只不过是所有类型对ado操作的各种方法的封装,所以本

[Python-MATLAB] 在Python中调用MATLAB的API

可以参考官方的说明文档: http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html MATLAB Engine API的使用文档: http://cn.mathworks.com/help/matlab/matlab-engine-for-python.html 原材料: 1.MATLAB 2015a  32位的 2.Python 2.7.13    32位