知一的指纹

Peewee源码解读

通过select查询语句引入

1
2
3
4
5
6
from model.db.tb1 import Tb1
query = Tb1.select().where(Tb1.id > 0)
print "select query:", query.sql()[0] % tuple(query.sql()[1])
print "query result:"
for item in query.dicts().execute():
print item
select query: SELECT `t1`.`id`, `t1`.`name`, `t1`.`age`, `t1`.`create_time` FROM `tb1` AS t1 WHERE (`t1`.`id` > 0)
query result:
{'age': 12, 'create_time': datetime.datetime(2017, 2, 5, 0, 14, 6), 'id': 1, 'name': u'test'}
{'age': 20, 'create_time': datetime.datetime(2017, 2, 5, 0, 14, 55), 'id': 2, 'name': u'zhang'}

分析每一次调用

peewee_select_query
其中Tb1是我们定义的一个Model

1
2
3
4
5
6
7
8
9
10
11
12
13
class Tb1(XYZ_DB.Model):
"""
"""
id = PrimaryKeyField()
name = CharField()
age = IntegerField()
create_time = DateTimeField()

class Meta(object):

"""表配置信息
"""
db_table = "tb1"

peewee对Model类指定BaseModel元类

1
class Model(with_metaclass(BaseModel)):

未完待续

Jupyter Markdown

noogel wechat
文章会同步推送到公众号,欢迎关注!
如果此文章能给您带来小小的提升,不妨小额赞赏我一下,以鼓励我写出更好的文章!