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()
<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
<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,布布扣,bubuko.com
原文地址:http://blog.csdn.net/hzrandd/article/details/32133175