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}
]
极客教程