Section A.11. Changing Calls to byName Methods

A.11. Changing Calls to byName Methods

SQLObject defines a number of useful methods on your classes such as select, to retrieve many objects based on query parameters, and get, to retrieve a single object based on its primary key. SQLAlchemy gives you those class methods.

For every column that you declare as an "alternateID" in SQLObject, SQLObject creates a class method called byColumnName. The idiom used in SQLAlchemy is different: There is a class method called get_by and you pass in a keyword parameter to retrieve a specific instance by that alternate ID. You have two options:

  1. You can change all the calls to byColumnName. In SQLObject you say obj = YourClass.byColumnName(value_to_find). In SQLAlchemy, you say obj = YourClass.get_by(columnName = value_to_find).

  2. You can add a byColumnName method to your class:

    class Products(ActiveMapper):         class mapping:             productCode = column(String(20), unique=True)         @classmethod         def byProductCode(cls, code):             return cls.get_by(productCode=code)

Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: