Chapter A.4. More about ActiveMapper

The SQLAlchemy documentation covers SQLAlchemy’s features in detail, but it does not cover the “extensions”, and ActiveMapper is an extension. Luckily, there is not much you need to know about ActiveMapper. In SQLAlchemy, when you define a table, you typically do something like this:

    products = Table("products", metadata,
        Column("product_id", Integer, primary_key=True),
        Column("product_code", String(20), unique=True),
        Column("description", String))

You can easily tell SQLAlchemy to map a class to that table:

    class Product(object):
        pass

    pmap = mapper(Product, products)

At that point, pmap offers methods for retrieving Product objects from the database. Here is the equivalent table and class created ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.