On Wed, Sep 11, 2013 at 06:07:38PM +0400, Oleg Broytman <p...@phdru.name> wrote:
> On Wed, Sep 11, 2013 at 10:25:31AM -0300, Georgio Barbosa 
> <georgio.barb...@gmail.com> 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.

   If you only need to set the default once you can override method
__init__ in your class, set the default and call parent's __init__. You
can also override .set() but simple assignment like

    row.title = new_value

doesn't call 'set(). If you want to catch all assignments events is the
only route.

Oleg.
-- 
     Oleg Broytman            http://phdru.name/            p...@phdru.name
           Programmers don't die, they just GOSUB without RETURN.

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. Consolidate legacy IT systems to a single system of record for IT
2. Standardize and globalize service processes across IT
3. Implement zero-touch automation to replace manual, redundant tasks
http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to