Fork me on GitHub

Twistar: Twisted Active Record

Twistar is a Python implementation of the active record pattern (also known as an object-relational mapper or ORM) that uses the Twisted framework’s RDBMS support to provide a non-blocking interface to relational databases.

Twistar currently features:

Quick Example For Those In A Rush

For this example, assume that there is a table named “users” with varchar columns for first_name and last_name and an int age column.

#!/usr/bin/env python
from twisted.enterprise import adbapi
from twistar.registry import Registry
from twistar.dbobject import DBObject
from twisted.internet import reactor

class User(DBObject):

def done(user):
     print "A user was just created with the name %s" % user.first_name

# Connect to the DB
Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user="twistar", passwd="apass", db="twistar")

# make a user
u = User()
u.first_name = "John"
u.last_name = "Smith"
u.age = 25

# Or, use this shorter version:
u = User(first_name="John", last_name="Smith", age=25)

# save the user

Then, finding this user is easy:

def found(users):
    print "I found %i users!" % len(users)
    for user in users:
        print "User: %s %s" % (user.first_name, user.last_name)

u = User.findBy(first_name="John", age=25).addCallback(found)

This is a very simple example - see the Examples and User Documentation for more complicated examples and additional uses.



One or more of the following Python database interfaces that implement the DBAPI PEP must first be installed:


Simply run:

pip install twistar

Unit Tests

Simply run:

trial twistar


To get debugging information (all queries executed) using twisted.python.log, just use:

from twistar.dbconfig.base import InteractionBase
InteractionBase.LOG = True


Questions and complaints Send questions to