title: Peewee category: page slug: peewee sortorder: 0908 toc: False sidebartitle: Peewee meta: Peewee is a object-relational mapper (ORM) implementation for bridging relational data and Python objects.
Peewee
Peewee (source code) is a object-relational mapper (ORM) implementation for bridging data stored in relational database tables with Python objects.
What makes Peewee a useful ORM?
Peewee can be an easier library to wrap your head around than SQLAlchemy and other ORMs. It is designed to be easier to hack on and understand, similar to how Bottle is a smaller, one-file web framework compared to the comprehensive Django framework. If you are just getting started with web development, it may be worth using Peewee for your database mapping and operations, especially if you use a microframework such as Flask or Bottle.
Peewee can be used with pretty much any web framework (although using it with Django would currently be complicated due to its tight built-in ORM coupling) or without a web framework. In the latter case Peewee is good for pulling data out of a relational database in a script or Jupyter notebook.
Any of the common relational database backends such as PostgreSQL, MySQL or SQLite are supported, although a database driver is still required. The chart below shows a few example configurations that could use Peewee as an ORM.
How does Peewee compare to other Python ORMs?
The analogy used by the core Peewee author is that Peewee is to SQLAlchemy as SQLite is to PostgreSQL. An ORM does not have to work for every exhaustive use case in order to be useful.
Peewee resources
Peewee is a much newer library than several other Python ORMs. For example, Peewee’s first public commit was in 2010, compared to 2005 for SQLAlchemy. The project is still over five years old though and matured substantially in development during that time. However, there are typically less resources and examples available to demonstrate how to use Peewee in your projects than some other ORMs that have been around for a longer period of time.
Many of the best resources come from the project’s author, Charles Leifer, on his blog and on the official site. There are also hundreds of questions answered on the Stack Overflow peewee tag, so as usual that can be a rich source of examples for your Peewee-powered Python applications.
Managing database connections with Peewee explains the connection pool and ExecutionContext of the ORM.
An encrypted command-line diary with Python is an awesome walkthrough explaining how to use SQLite, SQLCipher and Peewee to create an encrypted file with your contents, diary or otherwise.
An Intro to Peewee – Another Python ORM is a short tutorial that walks through creating a database model mapping, adding data, deleting records and querying fields.
Introduction to peewee uses an example public dataset, loads it into a SQLite database and shows how to query it using Peewee.
Shortcomings in the Django ORM and a look at Peewee from the author of the Peewee ORM explains how some of the design decisions made in Peewee were in reaction to parts of the Django ORM that didn’t work so well in practice.
The official Peewee quickstart documentation along with the example Twitter clone app will walk you through the ins and outs of your first couple Peewee-powered projects.
Flask and Peewee 101 has some basic code for querying with Peewee and populating a drop-down in a Jinja2 template. Note that the Flask-peewee extension is no longer maintained, although you do not need to use it to work with both Flask and Peewee in an application.
How to make a Flask blog in one hour or less is a well written tutorial that uses the Peewee ORM instead of SQLAlchemy for the blog back end.
These posts on querying the top item by group with Peewee ORM and querying the top N objects per group with Peewee ORM provide working examples on how to properly query your data via Peewee.
There was a good discussion in a Python subreddit thread about the differences between Peewee and SQLAlchemy. Charles Leifer even chimed in to add his own fair assessment of the differences in the ORMs.