您的位置:首页 > 编程语言 > Python开发

python轻量级ORM---peewee之API

2014-06-18 15:54 302 查看
1.classmethods suchas 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.
Exampleshowing 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:Thismethod 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 peewee api database