Database

We’re done with our database, let’s close the connection:

  1. db.close()

In an actual application, there are some established patterns for how you would manage your database connection lifetime. For example, a web application will typically open a connection at start of request, and close the connection after generating the response. A connection pool can help eliminate latency associated with startup costs.

To learn about setting up your database, see the Database documentation, which provides many examples. Peewee also supports configuring the database at run-time as well as setting or changing the database at any time.

Working with existing databases

If you already have a database, you can autogenerate peewee models using pwiz, a model generator. For instance, if I have a postgresql database named charles_blog, I might run:

  1. python -m pwiz -e postgresql charles_blog > blog_models.py