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 wouldmanage your database connection lifetime. For example, a web application willtypically open a connection at start of request, and close the connection aftergenerating the response. A connection pool can helpeliminate 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-timeas 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 usingpwiz, a model generator. For instance, if I have a postgresql database namedcharles_blog, I might run:

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