Retrieving row tuples / dictionaries / namedtuples

Sometimes you do not need the overhead of creating model instances and simplywant to iterate over the row data without needing all the APIs providedModel. To do this, use:

  1. stats = (Stat
  2. .select(Stat.url, fn.Count(Stat.url))
  3. .group_by(Stat.url)
  4. .tuples())
  5.  
  6. # iterate over a list of 2-tuples containing the url and count
  7. for stat_url, stat_count in stats:
  8. print(stat_url, stat_count)

Similarly, you can return the rows from the cursor as dictionaries usingdicts():

  1. stats = (Stat
  2. .select(Stat.url, fn.Count(Stat.url).alias('ct'))
  3. .group_by(Stat.url)
  4. .dicts())
  5.  
  6. # iterate over a list of 2-tuples containing the url and count
  7. for stat in stats:
  8. print(stat['url'], stat['ct'])