首页 > 代码库 > python轻量级ORM---peewee之API
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);">
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。