peewee — peewee v0.6.0 documentation

peewee v0.6.0 documentation

# peewee

- Table of Contents
- index
  next

# peewee

- a small orm
- written in python
- provides a lightweight querying interface over sql
- uses sql concepts when querying, like joins and where clauses

Examples:

    # a simple query selecting a user
    User.get(username='charles')

    # get the staff and super users
    editors = User.select().where(Q(is_staff=True) | Q(is_superuser=True))

    # get tweets by editors
    Tweet.select().where(user__in=editors)

    # how many active users are there?
    User.select().where(active=True).count()

    # paginate the user table and show me page 3 (users 41-60)
    User.select().order_by(('username', 'asc')).paginate(3, 20)

    # order users by number of tweets
    User.select({
        User: ['*'],
        Tweet: [Count('id', 'num_tweets')]
    }).group_by('id').join(Tweet).order_by(('num_tweets', 'desc'))

## Why?

peewee began when I was working on a small app in flask and found myself writing lots of queries and wanting a very simple abstraction on top of the sql. I had so much fun working on it that I kept adding features. My goal has always been, though, to keep the implementation incredibly simple. I’ve made a couple dives into django’s orm but have never come away with a deep understanding of its implementation. peewee is small enough that its my hope anyone with an interest in orms will be able to understand the code without too much trouble.

## Contents:

- Installing peewee
- Some typical usage scenarios
  - Creating a database connection and tables
  - Using with MySQL
  - Creating a new record
  - Getting a single record
  - Selecting some records
  - Filtering records
  - Sorting records
  - Paginating records
  - Counting records
- Example app
  - Running the example
  - Diving into the code
    - Models
    - Creating the initial tables
    - Connecting to the database
    - Doing queries
    - Creating new objects
    - Doing subqueries
- Model API (smells like django)
  - Creating tables
  - Model instances
  - Model options
  - Model methods
- Querying API
  - Constructing queries
  - Where clause
    - Performing advanced queries
  - Query evaluation
  - QueryResultWrapper
  - SelectQuery
  - UpdateQuery
  - DeleteQuery
  - InsertQuery
  - RawQuery

# Indices and tables

- Index
- Module Index
- Search Page