> you can associate the instance event with the mapper() callable or Mapper class, and it will take effect for all mapped classes.
Using 0.8 you can also associate the listener with a common base class, such as your declarative Base, or even a mixin that's common to some subset of classes.
That sounds even nicer, and since I'm just starting out with no legacy code I've been meaning to try dropping 0.8 in even before it is final. It sounds like I could just do something like event.listen(MyDeclarativeSubclass, "load", myStamperFunc).
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/rQwgslRRDGsJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
On Friday, January 11, 2013 2:34:09 AM UTC-4, Michael Bayer wrote:
> you can associate the instance event with the mapper() callable or Mapper class, and it will take effect for all mapped classes.I think that would work for my case, although I'm a little fuzzy as to the exact syntax to provide the mapper reference for event.listen - I've just been using declarative and haven't dealt with mappers directly - each of my classes would have a different mapper instance, and I'd have to attach to each (no benefit over class attachment)? Or using the Mapper class itself would trigger for anything mapped, and I'd have to discriminate in the handler for classes of interest, or create a Mapper subclass and somehow have my declarative_base subclass use it?
in theory. It's new stuff and was very tricky to get it to work, so feel free to send a brief test along.
On Friday, January 11, 2013 11:25:06 AM UTC-4, Michael Bayer wrote:in theory. It's new stuff and was very tricky to get it to work, so feel free to send a brief test along.Here's a minimal example I quickly put together - it retrieves from the database, but the handler doesn't seem to fire, and so the print statement on the second-last line fails. I'm using the standard MySQLdb with MySQL 5.0.43 (sigh, they promise to update soon) - the server is on a Debian box, and the client code is running in Python 1.6.4 on Windows 7 (it's actually the 64-bit Python interpreter bundled with the Maya graphics package):from sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy import Table, Column, Integer, event, create_enginefrom sqlalchemy.orm import sessionmakerdef My_load_instance_handler(instance, context):instance.itsProduction = None #should add itsProduction attribute to all loaded instancesprint "stamped loaded instance with production"AlchemyBase = declarative_base()event.listen(AlchemyBase, "load", My_load_instance_handler)
whoops, forgot the propagate flag:event.listen(Base, "load", My_load_instance_handler, propagate=True)