<p>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 <ahref="/bottle.html">Bottle</a> is a smaller, one-file
<ahref="/web-frameworks.html">web framework</a> compared to the comprehensive
<ahref="/django.html">Django</a> 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.</p>
<p>Peewee can be used with pretty much any web framework (although using it
with <ahref="/django.html">Django</a> 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. </p>
<p>Any of the common relational database backends such as
<ahref="/postgresql.html">PostgreSQL</a>, <ahref="/mysql.html">MySQL</a> or
<ahref="/sqlite.html">SQLite</a> are supported, although a database driver is
still required. The chart below shows a few example configurations
that could use Peewee as an ORM.</p>
<p><imgsrc="/img/sqlalchemy-orm-example.png" width="100%" alt="Example SQLAlchemy configurations with different web frameworks." class="technical-diagram"></a></p>
<divclass="well see-also">Peewee is an implementation of the <ahref="/object-relational-mappers-orms.html">object-relational mapping (ORM)</a> concept. Learn more in the <ahref="/data.html">data</a> chapter or view <ahref="/table-of-contents.html">all topics</a>.</div>
<h2>How does Peewee compare to other Python ORMs?</h2>
<p>The analogy used by the core Peewee author is that Peewee
is to <ahref="/sqlalchemy.html">SQLAlchemy</a> as <ahref="/sqlite.html">SQLite</a> is to
<ahref="/postgresql.html">PostgreSQL</a>. An
<ahref="/object-relational-mappers-orms.html">ORM</a> does not have to work for
every exhaustive use case in order to be useful.</p>
<h3>Peewee resources</h3>
<p>Peewee is a much newer library than several other Python ORMs.
For example, Peewee's <ahref="https://github.com/coleifer/peewee/commit/4fd56bd35c27861337861233a7f362474157af57">first public commit was in 2010</a>,
compared to
<ahref="https://github.com/zzzeek/sqlalchemy/commit/ec052c6a1f1fb0236bd367c510d82f076cb67bc9">2005 for SQLAlchemy</a>.
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.</p>
<p>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
so as usual that can be a rich source of examples for your Peewee-powered
Python applications.</p>
<ul>
<li>
<p><ahref="http://charlesleifer.com/blog/managing-database-connections-with-peewee/">Managing database connections with Peewee</a>
explains the connection pool and ExecutionContext of the ORM.</p>
</li>
<li>
<p><ahref="http://charlesleifer.com/blog/dear-diary-an-encrypted-command-line-diary-with-python/">An encrypted command-line diary with Python</a>
is an awesome walkthrough explaining how to use SQLite, SQLCipher and
Peewee to create an encrypted file with your contents, diary or otherwise.</p>
</li>
<li>
<p><ahref="http://www.blog.pythonlibrary.org/2014/07/17/an-intro-to-peewee-another-python-orm/">An Intro to Peewee – Another Python ORM</a>
is a short tutorial that walks through creating a database model mapping,
adding data, deleting records and querying fields.</p>
</li>
<li>
<p><ahref="http://jonathansoma.com/tutorials/webapps/intro-to-peewee/">Introduction to peewee</a>
uses an example public dataset, loads it into a <ahref="/sqlite.html">SQLite</a>
database and shows how to query it using Peewee.</p>
</li>
<li>
<p><ahref="http://charlesleifer.com/blog/shortcomings-in-the-django-orm-and-a-look-at-peewee-a-lightweight-alternative/">Shortcomings in the Django ORM and a look at Peewee</a>
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
is no longer maintained, although you do not need to use it to work with
both Flask and Peewee in an application.</p>
</li>
<li>
<p><ahref="http://charlesleifer.com/blog/how-to-make-a-flask-blog-in-one-hour-or-less/">How to make a Flask blog in one hour or less</a>
is a well written tutorial that uses the
<ahref="https://peewee.readthedocs.org/en/latest/">Peewee ORM</a> instead of
SQLAlchemy for the blog back end.</p>
</li>
<li>
<p>These posts on
<ahref="http://charlesleifer.com/blog/techniques-for-querying-lists-of-objects-and-determining-the-top-related-item/">querying the top item by group with Peewee ORM</a>
and
<ahref="http://charlesleifer.com/blog/querying-the-top-n-objects-per-group-with-peewee-orm/">querying the top N objects per group with Peewee ORM</a>
provide working examples on how to properly query your data via Peewee.</p>
<ahref="http://www.deploypython.com/"><imgsrc="/img/sponsored/fsp-deployment-guide.png" alt="The Full Stack Python Guide to Deployments" width="100%"></a>
<pstyle="font-size: .8em; margin-top: 10px;">Searching for a complete, step-by-step deployment walkthrough? Learn more about <ahref="http://www.deploypython.com/">The Full Stack Python Guide to Deployments book</a>.