Featured image of post Peewee ORM In a Nutshell

Peewee ORM In a Nutshell


What the Heck is Peewee?

Peewee is a tiny but powerful Object-Relational Mapping (ORM) library for Python. It simplifies database interactions by letting you write Pythonic code instead of raw SQL queries.

Unlike heavyweight ORMs like SQLAlchemy, Peewee is small, fast, and easy to learn, making it perfect for small to medium-sized applications.

Why Should You Use Peewee?

  • Lightweight – Less bloat, faster performance.
  • Easy to Use – Simple, intuitive API.
  • Supports Multiple Databases – SQLite, MySQL, PostgreSQL, you name it!
  • Built-in Migrations – No more manual schema changes.
  • Great for Beginners – Minimal setup and easy to grasp.

Installing Peewee

First things first—let’s get this thing installed. If you don’t have Peewee yet, just run:

1
pip install peewee

If you’re feeling fancy and need MySQL or PostgreSQL support, install with:

1
pip install peewee psycopg2-binary mysql-connector-python

Setting Up a Simple Database

Let’s start with SQLite (because it’s the easiest to set up).

Define Your Database and Models

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from peewee import *

# Create a database instance (SQLite for now)
db = SqliteDatabase('my_database.db')

# Define a model
class BaseModel(Model):
    class Meta:
        database = db  # This model uses the "db" database

class User(BaseModel):
    username = CharField(unique=True)
    email = CharField(unique=True)
    joined_at = DateTimeField(default=datetime.datetime.now)

# Create the tables
db.connect()
db.create_tables([User])

Boom! We just defined a SQLite database and a User model.


Adding Some Data

Let’s toss in some users:

1
2
3
# Insert data
User.create(username='john_doe', email='john@example.com')
User.create(username='jane_doe', email='jane@example.com')

Now we have users in our database! That was easy, right?


Querying Data

Peewee makes querying stupidly simple:

1
2
3
4
# Fetch all users
users = User.select()
for user in users:
    print(user.username, user.email)

Want a single user?

1
2
user = User.get(User.username == 'john_doe')
print(user.email)  # Output: john@example.com

Need filtering? No problem!

1
users = User.select().where(User.username.contains('doe'))

Updating and Deleting Data

Updating is also a breeze:

1
2
query = User.update(email='new_email@example.com').where(User.username == 'john_doe')
query.execute()

And when it’s time to say goodbye:

1
2
user = User.get(User.username == 'john_doe')
user.delete_instance()

Peewee vs Other ORMs

Let’s compare Peewee with some other popular ORMs:

FeaturePeeweeSQLAlchemyDjango ORM
ComplexityLowHighMedium
Setup TimeFastSlowMedium
PerformanceFastSlowerMedium
Query PowerGoodExcellentGood
Best forSmall/MediumLarge AppsDjango Apps

If you want a lightweight, simple ORM, Peewee is a solid choice!


Using Peewee with MySQL or PostgreSQL

Switching databases is easy—just change the database instance:

1
2
3
4
5
# For MySQL
db = MySQLDatabase('my_database', user='root', password='password', host='localhost')

# For PostgreSQL
db = PostgresqlDatabase('my_database', user='postgres', password='password', host='localhost')

Then, just update the database in your models and you’re good to go!



Key Ideas

Key IdeaSummary
What is Peewee?A lightweight ORM for Python
InstallationInstall with pip install peewee
Setting up SQLiteDefine models and create tables
Querying dataUse .select(), .get(), .where()
Updating and DeletingUse .update() and .delete_instance()
Comparison with ORMsLightweight alternative to SQLAlchemy
Multi-DB SupportSupports SQLite, MySQL, PostgreSQL

References