Re: [SQLObject] Saving a rol with a derivative atribute
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Oleg B. <ph...@ph...> - 2013-09-11 14:08:11
|
Hi! On Wed, Sep 11, 2013 at 10:25:31AM -0300, Georgio Barbosa <geo...@gm...> wrote: > I would like to know if it is possible to create a model class with a > default attribute derived from another. > > An example: > > class Post(SQLObject): > *title = StringCol()* > content = StringCol() > create_time = DateTimeCol(default=datetime.now) > tags = RelatedJoin('Tag') > author = ForeignKey('Author') > *slug = StringCol(default=title.replace(" ", "-"))* It is possible but in a rather complex non-obvious way. You have to use events (signals); unfortunately that gem of SQLObject is underdocumented (to say mildly), so you need to consult the code. See http://sqlobject.org/SQLObject.html#events-signals for the starting point. Read events.py and lookup main.py to see how signals are sent and how SQLObject uses returned values. You need to catch RowCreateSignal and RowUpdateSignal -- they are sent before INSERT and UPDATE correspondingly -- and update values to your need in your signal handlers. It would be great if after you learn the details of events you sent a patch to improve documentation. Or any other patch -- code, documentation, tests -- any help will be greatly appreciated. Oleg. -- Oleg Broytman http://phdru.name/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |