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);">


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值