On Feb 21, 2014, at 2:24 PM, Tino Dai <obe...@gmail.com> wrote:

> Hi All,
> 
>        I having some problems with having the event listen fire when I'm 
> setting the attributes via a query.
> 
> Base = declarative_base()
> lisDb = create_engine(Schema.lisDbstring())
> Base.metadata = MetaData(bind=lisDb, schema=Schema.lisSchema())
> lisSession = sessionmaker(lisDb)
> 
> easternTz = timezone('US/Eastern')
> 
> def eastern(lis_date):
>     return easternTz.localize(lis_date)
> 
> def convert_timezone_aware(target, value, oldvalue, initiator):
>     print "Got here"
>     return eastern(value)
> 
> event.listen(BdsMeasure.mod_sysdat, 'set', convert_timezone_aware, retval = 
> True)
> 
> # Below doesn't work
> b = lisSession().query(BdsMeasure).filter(BdsMeasure.congress == '113', 
> BdsMeasure.bill_number == 'HC00001').first()  
> print b.mod_sysdat
> 
> setattr(b, 'mod_sysdat', datetime.now())    # This however does
> print b.mod_sysdat
> 
> Can somebody shed some light on why the first setting of mod_sysdat is not 
> working (see above) and how to fix it. I'm running 0.7 due to legacy code 
> that we have not changed yet.

attribute events are listening specifically for changes made to an object in 
within the user application, which is not the same as the object being loaded 
from the database.  

To establish a listener that intercepts when an object is loaded, you can use 
the load event: 
http://docs.sqlalchemy.org/en/rel_0_9/orm/events.html#sqlalchemy.orm.events.InstanceEvents.load
  or reconstructor: 
http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html?highlight=reconstructor#constructors-and-object-initialization.
   Within this event you’d look to the specific attributes you’re concerned 
with and handle them there.






> 
> Thanks,
> 'Tino
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to