Introduction
Working with a database is an essential part of any modern application. Python offers many ORM libraries that make it easy to interact with SQL databases. One of the simplest and most compact is Peewee. It's perfect for small projects, scripts, and even microservices.
Peewee – a lightweight ORM that provides everything you need to define models, execute queries, handle transactions, and integrate with web frameworks — all while being easy to learn and implement.
How ORMs Work and Where Peewee Fits In
What is an ORM?
ORM (Object-Relational Mapping) is a technology that lets you work with databases using programming language objects. Instead of writing SQL queries, you create and interact with Python classes, and the ORM translates that into SQL.
ORMs provide an abstraction layer over SQL, making code more readable and less error-prone. Key benefits of using an ORM include:
- Automatic SQL query generation
- Protection against SQL injection
- Portability across different database systems
- Convenient Python object manipulation
When to Choose Peewee
Peewee is an excellent choice in these scenarios:
- When you need a simple, lightweight tool
- For small or experimental projects
- When fast development without complex configuration matters
- For prototyping and small web applications
- For desktop apps with a local database
Installation and Basic Setup
Installing Peewee
Install Peewee using pip:
pip install peewee
For different database systems, you'll need additional packages:
# For PostgreSQL
pip install psycopg2-binary
# For MySQL
pip install PyMySQL
# For SQLite (included with Python)
# no additional packages required
Connecting to a Database
Peewee supports several database types:
from peewee import SqliteDatabase, PostgresqlDatabase, MySQLDatabase
# SQLite
db = SqliteDatabase('my_database.db')
# PostgreSQL
db = PostgresqlDatabase('mydb', user='username', password='password', host='localhost', port=5432)
# MySQL
db = MySQLDatabase('mydb', user='username', password='password', host='localhost', port=3306)
Creating Data Models
Basic Model
from peewee import Model, CharField, DateField, IntegerField, TextField
class BaseModel(Model):
class Meta:
database = db
class Author(BaseModel):
name = CharField(max_length=100)
birth_date = DateField()
bio = TextField(null=True)
class Book(BaseModel):
title = CharField(max_length=200)
isbn = CharField(max_length=13, unique=True)
pages = IntegerField()
publication_year = IntegerField()
author = ForeignKeyField(Author, backref='books')
Creating Tables
db.connect()
db.create_tables([Author, Book])
Field Types in Peewee
Core Field Types
| Field Type | Description | Parameters |
|---|---|---|
| CharField | String with length limit | max_length, unique, null |
| TextField | Unlimited text | null |
| IntegerField | Integer number | null, unique |
| FloatField | Floating-point number | null, unique |
| BooleanField | Boolean value | null, default |
| DateField | Date | null, default |
| DateTimeField | Date and time |