peewee 一个轻量级的ORM(一)
2012-09-30 03:10
267 查看
peewee是一个轻量级的ORM,sf、github上都有。用的是大名鼎鼎的sqlalchemy内核,做了一个简单的包装,用纯python编写,显得十分轻便。
废话不多说,上 直接sudo pip install peewee即可。
[python]
view plaincopy
from peewee import *
db = SqliteDatabase('test.db')#peewee.SqliteDatabase
# create a base model class that our application's models will extend,这样我们后面的blog与entry就链接的是同一个数据库了。这个是从django借鉴来的
class BaseModel(Model):
class Meta:
database = db
class Blog(BaseModel):#创建表,Blog:表名,name:字段,CharField:字段类型,其实就是varchar。peewee.CharField
name = CharField() # <-- VARCHAR
class Entry(BaseModel):
headline = CharField()
content = TextField() # <-- TEXT
pub_date = DateTimeField() # <-- DATETIME
blog = ForeignKeyField(Blog) # <-- INTEGER referencing the Blog table
注意:严格来说,不是一定要connect()的,但这是一个好习惯
Use the
Model.create() classmethod:
[python]
view plaincopy
>>> blog = Blog.create(name='Funny pictures of animals blog')
>>> entry = Entry.create(
... headline='maru the kitty',
... content='http://www.youtube.com/watch?v=xdhLQCYQ-nQ',
... pub_date=datetime.datetime.now(),
... blog=blog
... )
>>> entry.blog.name
'Funny pictures of animals blog'
或者是仿照编程语言的方式进行实例化:
[python]
view plaincopy
>>> blog = Blog()#blog就是我们实例化的对象,而Blog就是刚才声明的class
>>> blog.name = 'Another sweet blog'
>>> blog.save()
[python]
view plaincopy
>>> entry.blog.name
'Funny pictures of animals blog'
还可以逆转过来:
[python]
view plaincopy
>>> for entry in blog.entry_set:#这个entry_set没太搞懂
... print entry.headline
...
maru the kitty
在我们这个peewee引擎中,entry_set是一个 SelectQuery:
meta是给引擎提示。引擎要执行一个sql语句的时候,要在那个数据库上执行呢,那就找到这个表所在的class继承的是那个元类
Note
Meta类中除了定义database之外还有其他几个属性。如下
database: 确定要用那个库
db_table: the name of the database table this model maps to,映射到哪张表
indexes: 一个索引的列表
ordering: a sequence of columns to use as the default ordering for this model
pk_sequence: 主键的名称队列(如果后端支持这种列表的话,peewee 可能会自动创建主键).
下面举一些例子
Specifying indexes:
Example of ordering:
save([force_insert=False])到底是save还是creating还是updating,这取决于这个表有木有主键。倘若force_insert设置为True,顾名思义,无论如何都会进行插入操作
example:
[python]
view plaincopy
>>> some_obj.title = 'new title' # <-- does not touch the database
>>> some_obj.save() # <-- 数据持久化
classmethod
create(**attributes)
Create an instance of the Model with the given attributes set.
example:
[python]
view plaincopy
>>> user = User.create(username='admin', password='test')
delete_instance([recursive=False])
删除我们指定的例子。 如果外键设置成级连的,也会被删除. For more programmatic control,you can call with recursive=True, which will delete any non-nullablerelated models (those thatare nullable will be set to NULL).
example:
[python]
view plaincopy
>>> some_obj.delete_instance() # <-- it is gone forever
classmethod
filter(*args, **kwargs)#过滤操作,
这是一个很像django的方法. 在
filter() andSelectQuery.where()二者之中的区别就是filter()
支持链式过滤
This method is chainable:
classmethod
get(*args, **kwargs)
这个很是类型thinkphp中的find与findall,get就是只返回一条。
[python]
view plaincopy
>>> active = User.select().where(active=True)
>>> try:
... user = active.get(username=username, password=password)
... except User.DoesNotExist:
... user = None
classmethod get_or_create(**attributes)
返回以键值对做查询条件的结果,如果查询不到,就以该键值对创建一个实例
example:
[python]
view plaincopy
>>> CachedObj.get_or_create(key=key, val=some_val)
classmethod
select(query=None)
example:
[python]
view plaincopy
>>> User.select().where(active=True).order_by('username')
classmethod
update(**query)
example:
[python]
view plaincopy
>>> q = User.update(active=False).where(registration_expired=True)
>>> q.sql()
('UPDATE user SET active=? WHERE registration_expired = ?', [0, 1])
>>> q.execute() # <-- execute it
classmethod
delete(**query)
example:
[python]
view plaincopy
>>> q = User.delete().where(active=False)
>>> q.sql()
('DELETE FROM user WHERE active = ?', [0])
>>> q.execute() # <-- execute it
delelte语句的时候一定要有where语句。
classmethod
insert(**query)
example:
[python]
view plaincopy
>>> q = User.insert(username='admin', active=True, registration_expired=False)
>>> q.sql()
('INSERT INTO user (username,active,registration_expired) VALUES (?,?,?)', ['admin', 1, 0])
>>> q.execute()
1
classmethod
raw(sql, *params)
example:
[python]
view plaincopy
>>> q = User.raw('select id, username from users')
>>> for user in q:
... print user.id, user.username
classmethod create_table([fail_silently=False])
Create the table for the given model.创建表
example:
classmethoddrop_table([fail_silently=False])
注意,有外键连接的不会受此影响。
classmethod table_exists()
废话不多说,上 直接sudo pip install peewee即可。
[python]
view plaincopy
from peewee import *
db = SqliteDatabase('test.db')#peewee.SqliteDatabase
# create a base model class that our application's models will extend,这样我们后面的blog与entry就链接的是同一个数据库了。这个是从django借鉴来的
class BaseModel(Model):
class Meta:
database = db
class Blog(BaseModel):#创建表,Blog:表名,name:字段,CharField:字段类型,其实就是varchar。peewee.CharField
name = CharField() # <-- VARCHAR
class Entry(BaseModel):
headline = CharField()
content = TextField() # <-- TEXT
pub_date = DateTimeField() # <-- DATETIME
blog = ForeignKeyField(Blog) # <-- INTEGER referencing the Blog table
创建表
#建立连接 db.connect() # 建立表 Blog.create_table() Entry.create_table()
注意:严格来说,不是一定要connect()的,但这是一个好习惯
创建模型实例(实例化)
在我们的交互式脚本编辑器那试试吧,这样能立即看出效果Use the
Model.create() classmethod:
[python]
view plaincopy
>>> blog = Blog.create(name='Funny pictures of animals blog')
>>> entry = Entry.create(
... headline='maru the kitty',
... content='http://www.youtube.com/watch?v=xdhLQCYQ-nQ',
... pub_date=datetime.datetime.now(),
... blog=blog
... )
>>> entry.blog.name
'Funny pictures of animals blog'
或者是仿照编程语言的方式进行实例化:
[python]
view plaincopy
>>> blog = Blog()#blog就是我们实例化的对象,而Blog就是刚才声明的class
>>> blog.name = 'Another sweet blog'
>>> blog.save()
Traversing foriegn keys通过外键访问
比如我们上面那个例子[python]
view plaincopy
>>> entry.blog.name
'Funny pictures of animals blog'
还可以逆转过来:
[python]
view plaincopy
>>> for entry in blog.entry_set:#这个entry_set没太搞懂
... print entry.headline
...
maru the kitty
在我们这个peewee引擎中,entry_set是一个 SelectQuery:
>>> blog.entry_set <peewee.SelectQuery object at 0x151f510> >>> blog.entry_set.sql() ('SELECT * FROM entry WHERE blog_id = ?', [1])
Model options
还可用多几个数据库from peewee import * custom_db = SqliteDatabase('custom.db') class CustomModel(Model#这种元类 class Meta: database = custom_db ):
meta是给引擎提示。引擎要执行一个sql语句的时候,要在那个数据库上执行呢,那就找到这个表所在的class继承的是那个元类
Note
Meta类中除了定义database之外还有其他几个属性。如下
database: 确定要用那个库
db_table: the name of the database table this model maps to,映射到哪张表
indexes: 一个索引的列表
ordering: a sequence of columns to use as the default ordering for this model
pk_sequence: 主键的名称队列(如果后端支持这种列表的话,peewee 可能会自动创建主键).
下面举一些例子
Specifying indexes:
class Transaction(Model): from_acct = CharField() to_acct = CharField() amount = DecimalField() date = DateTimeField() class Meta: indexes = ( # create a unique on from/to/date (('from_acct', 'to_acct', 'date'), True),#索引,True的话,索引唯一,False则不唯一 # create a non-unique on from/to (('from_acct', 'to_acct'), False), )
Example of ordering:
class Entry(Model): title = CharField() body = TextField() created = DateTimeField() class Meta: # order by created date descending, then title ascending ordering = (('created', 'desc'), 'title')
Model methods对数据表操作的方法
class Modelsave([force_insert=False])到底是save还是creating还是updating,这取决于这个表有木有主键。倘若force_insert设置为True,顾名思义,无论如何都会进行插入操作
example:
[python]
view plaincopy
>>> some_obj.title = 'new title' # <-- does not touch the database
>>> some_obj.save() # <-- 数据持久化
classmethod
create(**attributes)
Parameters: | attributes – 键值对 |
---|
example:
[python]
view plaincopy
>>> user = User.create(username='admin', password='test')
delete_instance([recursive=False])
删除我们指定的例子。 如果外键设置成级连的,也会被删除. For more programmatic control,you can call with recursive=True, which will delete any non-nullablerelated models (those thatare nullable will be set to NULL).
example:
[python]
view plaincopy
>>> some_obj.delete_instance() # <-- it is gone forever
classmethod
filter(*args, **kwargs)#过滤操作,
Parameters: | args – a list of Q or Node objects kwargs – a mapping of column + lookup to value, e.g. “age__gt=55” |
---|---|
Return type: | SelectQuery with appropriate WHERE clauses |
filter() andSelectQuery.where()二者之中的区别就是filter()
支持链式过滤
>>> sq = Entry.filter(blog__title='Some Blog')
This method is chainable:
[python] view plaincopy>>> base_q = User.filter(active=True) >>> some_user = base_q.filter(username='charlie')
classmethod
get(*args, **kwargs)
Parameters: | args – a list of Q or Node objects kwargs – a mapping of column + lookup to value, e.g. “age__gt=55” |
---|---|
Return type | 返回实力或者一个Does Not Exist异常 |
[python] view plaincopy>>> user = User.get(username=username, password=password
[python]
view plaincopy
>>> active = User.select().where(active=True)
>>> try:
... user = active.get(username=username, password=password)
... except User.DoesNotExist:
... user = None
classmethod get_or_create(**attributes)
Parameters: | attributes –键值对 |
---|---|
Return type: | 查询结果实例 |
example:
[python]
view plaincopy
>>> CachedObj.get_or_create(key=key, val=some_val)
classmethod
select(query=None)
Return type: | a SelectQuery for the givenModel查询结果 |
---|
[python]
view plaincopy
>>> User.select().where(active=True).order_by('username')
classmethod
update(**query)
Return type: | an UpdateQuery for the givenModel更新 |
---|
[python]
view plaincopy
>>> q = User.update(active=False).where(registration_expired=True)
>>> q.sql()
('UPDATE user SET active=? WHERE registration_expired = ?', [0, 1])
>>> q.execute() # <-- execute it
classmethod
delete(**query)
Return type: | a DeleteQuery for the givenModel删除 |
---|
[python]
view plaincopy
>>> q = User.delete().where(active=False)
>>> q.sql()
('DELETE FROM user WHERE active = ?', [0])
>>> q.execute() # <-- execute it
delelte语句的时候一定要有where语句。
classmethod
insert(**query)
Return type: | an InsertQuery for the givenModel插入 |
---|
[python]
view plaincopy
>>> q = User.insert(username='admin', active=True, registration_expired=False)
>>> q.sql()
('INSERT INTO user (username,active,registration_expired) VALUES (?,?,?)', ['admin', 1, 0])
>>> q.execute()
1
classmethod
raw(sql, *params)
Return type: | a RawQuery for the givenModel使用原始的sql语句 |
---|
[python]
view plaincopy
>>> q = User.raw('select id, username from users')
>>> for user in q:
... print user.id, user.username
classmethod create_table([fail_silently=False])
Parameters: | fail_silently 如果这个值被设置为True,那么该方法就会在创建的时候先检查一个这个表的存在型. |
---|
example:
>>> database.connect() >>> SomeModel.create_table() # <-- creates the table for SomeModel
classmethoddrop_table([fail_silently=False])
Parameters: | fail_silently –如果是True,在删除之前会检查该表是否存在. |
---|
classmethod table_exists()
Return type: | 返回该数据库是否存在,返回bool值 |
---|
相关文章推荐
- peewee 一个轻量级的ORM(二)
- peewee 一个轻量级的ORM (三)
- peewee 一个轻量级的ORM (四)
- peewee 一个轻量级的ORM
- sql2java:一个古老但稳定的轻量级的ORM工具的使用说明
- 一个超轻量级的 ORM 框架
- python轻量级ORM---peewee
- ORM-HBASE 一个轻量级映射框架
- 一个超轻量级的 ORM 框架
- python轻量级ORM---peewee之API
- (1)知识准备【利用objective-c的runtime特性,结合FMDB实现一个轻量级的ORM】
- Ptypes一个开源轻量级的c++库,包括对一些I/O操作、网络通信、多线程和异常处理的封装
- 【开源.NET】 分享一个前后端分离的轻量级内容管理框架
- NET Core写了一个轻量级的Interception框架[开源]
- FreeImage轻量级图片库使用的一个问题
- [JAVA]打造一个缩水版的ORM映射(mih)-- beta
- 分享一个轻量级的简洁,清爽的Tab菜单
- OrmLite动态创建表,一个实体类创建多张表的的偏招
- 介绍一个轻量级的C++ Unit test 框架: TUT