On Aug 21, 2008, at 9:19 AM, GustaV wrote:

>
> Hi all!
> I'm looking for an easy way to get informed when someone append or
> remove an object from a specific one-to-many relation. For example to
> keep a count on the parent object, with no need to query the children.
> There are a lot of stuff about it, but I'm not sure how to do it
> really:
>
> - 1st :
> http://markmail.org/message/oyt57qx3247jdhgi#query:sqlalchemy 
> %20__sa_instrument_class__+page:1+mid:wz4wkumhvnnm74yw+state:results
> Does not seem to work on 0.5

this is the correct approach.  The names have changed per examples/ 
custom_attributes/custom_management.py .

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.orm.interfaces import AttributeExtension


class ReceiveEvents(AttributeExtension):
      def append(self, obj, child, initiator):
          print "append", obj, child

      def remove(self, obj, child, initiator):
          print "remove", obj, child

      def set(self, obj, child, oldchild, initiator):
          print "set", obj, child

listener = ReceiveEvents()

class SetListener(InstrumentationManager):
      def instrument_attribute(self, class_, key, inst):
          inst.impl.extensions.append(listener)

if __name__ == '__main__':

      m = MetaData()
      t = Table('t', m, Column('a', Integer, primary_key=True),  
Column('b', String))
      class C(object):
          __sa_instrumentation_manager__ = SetListener

      mapper(C, t)

      c1 = C()


      c1.a = 4
      c1.b = "test"

  
     

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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