On Wed, Sep 11, 2013 at 06:07:38PM +0400, Oleg Broytman <ph...@ph...> wrote:
> 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.
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/ ph...@ph...
Programmers don't die, they just GOSUB without RETURN.
|