4 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: Running initializati...
FromSent OnAttachments
Sanjay26 Feb 2007 04:18 
Sanjay28 Feb 2007 06:22 
Rick Morrison28 Feb 2007 08:26 
Sanjay02 Mar 2007 01:53 
Subject:[sqlalchemy] Re: Running initialization code after creating/loading an object
From:Rick Morrison (rick@gmail.com)
Date:02/28/2007 08:26:10 AM
List:com.googlegroups.sqlalchemy

Hi Sanjay,

Mapper Extension is the way to do this. I use one we call our "compiler" extension, it fires events before database insert, datase update and after database load; you can use these events to modify the object as needed. You can probably hack this into what you need:

class CompilerExtension(MapperExtension): """ Allows for "compilation" of sub-items into binary blobs stored on the parent """ def before_insert(self, mapper, connection, instance): instance.oninsert() return EXT_PASS def before_update(self, mapper, connection, instance): instance.onupdate() return EXT_PASS def populate_instance(self, mapper, selectcontext, row, instance, identitykey, isnew): if isnew: mapper.populate_instance(selectcontext, instance, row, identitykey, isnew) instance.onload() return None return EXT_PASS

The code for the "onload" is a bit tangled, because SA does not (yet) provide an "after" db load, just a "before". I've asked Mike for another "after" hook (bump, bump), but I think he may have forgotten (bump) about (bump) it (bump...).

Rick

On 2/28/07, Sanjay <skpa@gmail.com> wrote:

Hi All,

Needing help badly. Summarizing my problem: I need to run some initialization code after an object is retrieved from database. Say I have a table Person(first_name, last_name), I need that while the object is fetched, another instance variable named full_name is assigned as firstname + last_name.

Tried to use MapperExtension by going through the suggestions in some threads, in the FAQ and the doc, but could not secceed. Also, could not find out how to use __new__ in this scenario. Surely my noviceness, either in Python or SQLAlchemy.

Some sample code or specific guidence could help a lot.