Re: [SQLObject] creating tables at run time
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2004-02-16 17:50:23
|
maxwell hammer wrote: > Not sure if this is safe but this is what works > > from SQLObject import * > import new > import copy > > conn = MySQLConnection(user='test',db='test') > > class TMessageBoard(SQLObject): > _connection=conn > user_name=StringCol() > boardname=StringCol() > parent=IntCol() > joinColumn="MessageLists" > > def _getcolumns(self): > return [c.kw['name'] for c in self._columns] > > _getcolumns = classmethod(_getcolumns) > > > t = new.classobj( > 'fredy', > (TMessageBoard,), > { > > }) > print dir(t) > > print t > t.createTable(True) > and i end up with a table named fredy Yep, that's safe, and entirely equivalent to the class statement, so all these are exactly the same: class fredy(TMessageBoard): pass fredy = new.classobj('fredy', (TMessageBoard,), {}) fredy = type('fredy', (TMessageBoard,), {}) That type() can be used like this is poorly documented, but you can think of it that a class's class is "type", and you're calling the constructor for classes. Well, actually SQLObject's metaclass (class's class) is MetaSQLObject, but I believe type is smart enough to figure this out by looking at the superclasses (TMessageBoard,). Confusingly, the traditional use of type() differs from the constructor only by signature. Ian |