Selecting multiple records

We can use Model.select() to retrieve rows from the table. When you construct a SELECT query, the database will return any rows that correspond to your query. Peewee allows you to iterate over these rows, as well as use indexing and slicing operations:

  1. >>> query = User.select()
  2. >>> [user.username for user in query]
  3. ['Charlie', 'Huey', 'Peewee']
  4. >>> query[1]
  5. <__main__.User at 0x7f83e80f5550>
  6. >>> query[1].username
  7. 'Huey'
  8. >>> query[:2]
  9. [<__main__.User at 0x7f83e80f53a8>, <__main__.User at 0x7f83e80f5550>]

Select queries are smart, in that you can iterate, index and slice the query multiple times but the query is only executed once.

In the following example, we will simply call select() and iterate over the return value, which is an instance of Select. This will return all the rows in the User table:

  1. >>> for user in User.select():
  2. ... print(user.username)
  3. ...
  4. Charlie
  5. Huey
  6. Peewee

Note

Subsequent iterations of the same query will not hit the database as the results are cached. To disable this behavior (to reduce memory usage), call Select.iterator() when iterating.

When iterating over a model that contains a foreign key, be careful with the way you access values on related models. Accidentally resolving a foreign key or iterating over a back-reference can cause N+1 query behavior.

When you create a foreign key, such as Tweet.user, you can use the backref to create a back-reference (User.tweets). Back-references are exposed as Select instances:

  1. >>> tweet = Tweet.get()
  2. >>> tweet.user # Accessing a foreign key returns the related model.
  3. <tw.User at 0x7f3ceb017f50>
  4. >>> user = User.get()
  5. >>> user.tweets # Accessing a back-reference returns a query.
  6. <peewee.ModelSelect at 0x7f73db3bafd0>

You can iterate over the user.tweets back-reference just like any other Select:

  1. >>> for tweet in user.tweets:
  2. ... print(tweet.message)
  3. ...
  4. hello world
  5. this is fun
  6. look at this picture of my food

In addition to returning model instances, Select queries can return dictionaries, tuples and namedtuples. Depending on your use-case, you may find it easier to work with rows as dictionaries, for example:

  1. >>> query = User.select().dicts()
  2. >>> for row in query:
  3. ... print(row)
  4. {'id': 1, 'username': 'Charlie'}
  5. {'id': 2, 'username': 'Huey'}
  6. {'id': 3, 'username': 'Peewee'}

See namedtuples(), tuples(), dicts() for more information.

Iterating over large result-sets

By default peewee will cache the rows returned when iterating over a Select query. This is an optimization to allow multiple iterations as well as indexing and slicing without causing additional queries. This caching can be problematic, however, when you plan to iterate over a large number of rows.

To reduce the amount of memory used by peewee when iterating over a query, use the iterator() method. This method allows you to iterate without caching each model returned, using much less memory when iterating over large result sets.

  1. # Let's assume we've got 10 million stat objects to dump to a csv file.
  2. stats = Stat.select()
  3. # Our imaginary serializer class
  4. serializer = CSVSerializer()
  5. # Loop over all the stats and serialize.
  6. for stat in stats.iterator():
  7. serializer.serialize_object(stat)

For simple queries you can see further speed improvements by returning rows as dictionaries, namedtuples or tuples. The following methods can be used on any Select query to change the result row type:

Don’t forget to append the iterator() method call to also reduce memory consumption. For example, the above code might look like:

  1. # Let's assume we've got 10 million stat objects to dump to a csv file.
  2. stats = Stat.select()
  3. # Our imaginary serializer class
  4. serializer = CSVSerializer()
  5. # Loop over all the stats (rendered as tuples, without caching) and serialize.
  6. for stat_tuple in stats.tuples().iterator():
  7. serializer.serialize_tuple(stat_tuple)

When iterating over a large number of rows that contain columns from multiple tables, peewee will reconstruct the model graph for each row returned. This operation can be slow for complex graphs. For example, if we were selecting a list of tweets along with the username and avatar of the tweet’s author, Peewee would have to create two objects for each row (a tweet and a user). In addition to the above row-types, there is a fourth method objects() which will return the rows as model instances, but will not attempt to resolve the model graph.

For example:

  1. query = (Tweet
  2. .select(Tweet, User) # Select tweet and user data.
  3. .join(User))
  4. # Note that the user columns are stored in a separate User instance
  5. # accessible at tweet.user:
  6. for tweet in query:
  7. print(tweet.user.username, tweet.content)
  8. # Using ".objects()" will not create the tweet.user object and assigns all
  9. # user attributes to the tweet instance:
  10. for tweet in query.objects():
  11. print(tweet.username, tweet.content)

For maximum performance, you can execute queries and then iterate over the results using the underlying database cursor. Database.execute() accepts a query object, executes the query, and returns a DB-API 2.0 Cursor object. The cursor will return the raw row-tuples:

  1. query = Tweet.select(Tweet.content, User.username).join(User)
  2. cursor = database.execute(query)
  3. for (content, username) in cursor:
  4. print(username, '->', content)