Peewee - 检索行元组/字典
可以在不创建模型实例的情况下迭代结果集。 这可以通过使用以下方法来实现 −
tuples() 方法。
dicts() 方法。
示例
要将 SELECT 查询中的字段数据作为元组集合返回,请使用 tuples() 方法。
qry=Contacts.select(Contacts.City, fn.Count(Contacts.City).alias('count'))
.group_by(Contacts.City).tuples()
lst=[]
for q in qry:
lst.append(q)
print (lst)
输出
输出如下 −
[
('Chennai', 1),
('Delhi', 2),
('Indore', 1),
('Mumbai', 1),
('Nagpur', 1),
('Nasik', 3),
('Pune', 1)
]
示例
获取字典对象的集合 −
qs=Brand.select().join(Item).dicts() lst=[] for q in qs: lst.append(q) print (lst)
输出
输出如下 −
[
{'id': 1, 'brandname': 'Dell', 'item': 1},
{'id': 2, 'brandname': 'Epson', 'item': 2},
{'id': 3, 'brandname': 'HP', 'item': 1},
{'id': 4, 'brandname': 'iBall', 'item': 3},
{'id': 5, 'brandname': 'Sharp', 'item': 2}
]

