Retrieving row tuples / dictionaries / namedtuples

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

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

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

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