peewee — peewee 2.8.0 documentation

# peewee

Peewee is a simple and small ORM. It has few (but expressive) concepts, making it easy to learn and intuitive to use.

- A small, expressive ORM
- Written in python with support for versions 2.6+ and 3.2+.
- built-in support for sqlite, mysql and postgresql
- numerous extensions available (postgres hstore/json/arrays, sqlite full-text-search, schema migrations, and much more).

Peewee’s source code hosted on GitHub.

New to peewee? Here is a list of documents you might find most helpful when getting started:

- Quickstart guide – this guide covers all the bare essentials. It will take you between 5 and 10 minutes to go through it.
- Guide to the various query operators describes how to construct queries and combine expressions.
- Field types table lists the various field types peewee supports and the parameters they accept. There is also an extension module that contains special/custom field types.

## Contents:

- Installing and Testing
  - Installing with git
  - Running tests
- Quickstart
  - Model Definition
  - Storing data
  - Retrieving Data
  - Working with existing databases
  - What next?
- Example app
  - Running the example
  - Diving into the code
  - More examples
- Additional Resources
- Contributing
  - Patches
  - Bugs
  - Questions
- Managing your Database
  - Creating a database connection and tables
  - Using Postgresql
  - Using SQLite
  - Using BerkeleyDB
  - Using MySQL
  - Connecting using a Database URL
  - Multi-threaded applications
  - Run-time database configuration
  - Dynamically defining a database
  - Connection Pooling
  - Read Slaves
  - Schema migrations
  - Generating Models from Existing Databases
  - Adding Request Hooks
  - Additional connection initialization
  - Advanced Connection Management
  - Using multiple databases
  - Database Errors
  - Automatic Reconnect
  - Logging queries
  - Generating skeleton code
  - Adding a new Database Driver
- Models and Fields
  - Fields
  - Creating model tables
  - Model options and table metadata
  - Indexes and Constraints
  - Non-integer Primary Keys, Composite Keys and other Tricks
  - Self-referential foreign keys
  - Circular foreign key dependencies
- Querying
  - Creating a new record
  - Bulk inserts
  - Updating existing records
  - Atomic updates
  - Deleting records
  - Selecting a single record
  - Create or get
  - Selecting multiple records
  - Filtering records
  - Sorting records
  - Getting random records
  - Paginating records
  - Counting records
  - Aggregating records
  - Retrieving Scalar Values
  - SQL Functions, Subqueries and “Raw expressions”
  - Security and SQL Injection
  - Window functions
  - Retrieving raw tuples / dictionaries
  - Returning Clause
- Query operators
  - Three valued logic
  - Adding user-defined operators
  - Expressions
- Foreign Keys
  - Traversing foreign keys
  - Joining tables
  - Implementing Many to Many
  - Self-joins
- Performance Techniques
  - Avoiding N+1 queries
  - Iterating over lots of rows
  - Speeding up Bulk Inserts
- Transactions
  - Context manager
  - Decorator
  - Nesting Transactions
  - Explicit transaction
  - Autocommit Mode
- API Reference
  - Models
  - Fields
  - Query Types
  - Database and its subclasses
  - Transaction, Savepoint and ExecutionContext
  - Metadata Types
  - Misc
- Playhouse, extensions to Peewee
  - Sqlite Extensions
  - apsw, an advanced sqlite driver
  - BerkeleyDB backend
  - Sqlcipher backend
  - Postgresql Extensions
  - DataSet
  - Django Integration
  - Fields
  - Generic foreign keys
  - Hybrid Attributes
  - Key/Value Store
  - Shortcuts
  - Signal support
  - pwiz, a model generator
  - Schema Migrations
  - Reflection
  - Database URL
  - CSV Utils
  - Connection pool
  - Read Slaves
  - Test Utils
  - pskel
  - Flask Utils
- Hacks
  - Top object per group
  - Top N objects per group
  - Writing custom functions with SQLite

## Note

If you find any bugs, odd behavior, or have an idea for a new feature please don’t hesitate to open an issue on GitHub or contact me.

# Indices and tables

- Index
- Module Index
- Search Page