Chapter 10.3.1. Getting a Single Object by ID

It is useful sometimes to just get a single row by ID. I do it constantly during interactive development, and you might also find it useful in your program. The ID will usually be stored as a foreign key or as a result of a query. SQLObject makes it as easy as possible. Just call the get() method of your model class with the ID:

import os, sys from sqlobject import * db_filename = os.path.abspath('test.db').replace(':\\', '|\\') sqlhub.processConnection = connectionForURI('sqlite:/' + db_filename) class Highlander(SQLObject): name = StringCol() motto = StringCol() Highlander.dropTable(ifExists=True) Highlander.createTable() Highlander(name='Connor Macleod', motto='There can be only one!') o = Highlander.get(1) ...

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.