On Dec 27, 2010, at 5:30 PM, lycovian wrote:

> A models __init__ method works great to setup attributes on the model
> that don't exist in the underlying table when creating a model.
> Unfortunately, if you are retrieving the model though the __init__
> method isn't triggered.  Is there something like an event that can be
> hooked such that code can be run when the model is instantiated via
> retrieval from the underlying DB?
> 
> I see events in the event system (looking at 0.7b1) but nothing that
> looks to be triggered when the model is loaded into memory.
> 
> If there isn't an event for this what would be the recommended way to
> accomplish this?  Thanks for any help that can be rendered.

the traditional way to get at this functionality since 0.5 is via 
@reconstructor:

        
http://www.sqlalchemy.org/docs/orm/mapper_config.html?highlight=reconstructor#constructors-and-object-initialization
        
http://www.sqlalchemy.org/docs/orm/mapper_config.html?highlight=reconstructor#sqlalchemy.orm.reconstructor

The event itself is public in 0.6 as MapperExtension.reconstruct_instance:

        
http://www.sqlalchemy.org/docs/orm/interfaces.html#sqlalchemy.orm.interfaces.MapperExtension.reconstruct_instance

in 0.7, the new interface publishes this event as "on_load()" and also 
introduces "on_refresh()":

        
http://www.sqlalchemy.org/docs/07/orm/events.html#sqlalchemy.orm.events.InstanceEvents.on_load


> 
> Cheers.
> 
> Mike
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to