Peewee

作者: 子非初心 | 来源:发表于2018-03-22 18:23 被阅读154次

peewee是一款轻量级、丰富的ORM(Object Relation Mapping,对象关系映射),支持postgresql、mysql和sqlite。

简单示例

当使用peewee开始一个项目,通常最好通过定义一个或者多个模型类开始你的数据模型。
from peewee import *
db = SqliteDatabase('people.db')

class Person(Model):

    name = CharField()
    birthday = DateField()
    is_relative = BooleanField() 
    class Meta:

        database = db #  这个模型使用 "people.db"数据库

大量的插件支持:

  • Postgresql HStore,Json,Arrays
  • SQLite全文搜索、自定义函数、虚拟表格
  • 结构迁移和model代码生成器
  • 连接池
  • 加密
  • 更多

示例

模型定义和Django、SQLAlchemy类似
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase
import datetime
 
db = SqliteExtDatabase('my_database.db')
 
class BaseModel(Model):
    class Meta:
        database = db
 
class User(BaseModel):
    username = CharField(unique=True)
 
class Tweet(BaseModel):
    user = ForeignKeyField(User, related_name='tweets')
    message = TextField()
    created_date = DateTimeField(default=datetime.datetime.now)
    is_published = BooleanField(default=True)

连接到数据库并创建表格和添加数据

db.connect()
db.create_tables([User, Tweet])
 
charlie = User.create(username='charlie')
huey = User(username='huey')
huey.save()
 
# No need to set `is_published` or `created_date` since they
# will just use the default values we specified.
Tweet.create(user=charlie, message='My first tweet')

查询

# A simple query selecting a user.
User.get(User.username == 'charles')
 
# Get tweets created by one of several users. The "<<" operator
# corresponds to the SQL "IN" operator.
usernames = ['charlie', 'huey', 'mickey']
users = User.select().where(User.username << usernames)
tweets = Tweet.select().where(Tweet.user << users)
 
# We could accomplish the same using a JOIN:
tweets = (Tweet
          .select()
          .join(User)
          .where(User.username << usernames))
 
# How many tweets were published today?
tweets_today = (Tweet
                .select()
                .where(
                    (Tweet.created_date >= datetime.date.today()) &
                    (Tweet.is_published == True))
                .count())
 
# Paginate the user table and show me page 3 (users 41-60).
User.select().order_by(User.username).paginate(3, 20)
 
# Order users by the number of tweets they've created:
tweet_ct = fn.Count(Tweet.id)
users = (User
         .select(User, tweet_ct.alias('ct'))
         .join(Tweet, JOIN.LEFT_OUTER)
         .group_by(User)
         .order_by(tweet_ct.desc()))
 
# Do an atomic update
Counter.update(count=Counter.count + 1).where(
    Counter.url == request.url)

相关文章

  • Peewee 使用

    Peewee系列:Peewee 使用Peewee使用之事务Peewee批量插入数据Peewee 使用(二)——增删...

  • Peewee使用之事务

    Peewee系列:Peewee 使用Peewee使用之事务Peewee批量插入数据Peewee 使用(二)——增删...

  • Peewee批量插入数据

    Peewee系列:Peewee 使用Peewee使用之事务Peewee批量插入数据Peewee 使用(二)——增删...

  • 轻量级ORM工具peewee初探

    peewee是什么 peewee是Python一个ORM框架,相比于著名的SQLAlchemy,peewee更为轻...

  • Peewee中文文档

    Peewee中文文档【一】:安装与测试 Peewee中文文档【二】:快速开始 Peewee中文文档【三】:应用实例...

  • python 操作 mysql

    python 操作 mysql python ORM peewee 安装:pip install peewee p...

  • SQLAlchemy选型实战

    peewee 与sqlalchemy选型对比 peewee 在github5000星sqlalchemy在gith...

  • python orm框架peewee

    1.准备 官方链接http://docs.peewee-orm.com/en/latest/peewee/api....

  • peewee的一个坑

    peewee是一个轻量级的DB peewee非常适合一般的处理,一般的存储和查询工作都能做到。今天遇到peewee...

  • Peewee批量插入数据

    参考 Peewee批量插入数据 使用原生的insert_many()方法,并放入事务中 经查阅文档,peewee支...

网友评论

      本文标题:Peewee

      本文链接:https://www.haomeiwen.com/subject/czfzqftx.html