The mixin by itself is not mapped so MyAddressMixin.address is just a Column
object, not the InstrumentedAttribute which accepts the "set" event.
So for this kind of thing you need to set up those events within an event of
their own:
from sqlalchemy.orm import mapper
@event.listens_for(mapper, "mapper_configured")
def _listen_address(m, cls_):
if issubclass(cls_, MyAddressMixin):
event.listen(cls_.address, 'set', always_foo, retval=True)
class Employee(Base, MyAddressMixin):
__tablename__ = 'employees'
id = Column(Integer, primary_key=True)
name = Column(String(100))
def always_foo(target, value, oldvalue, initiator):
return "foo"
print Employee(address="Adsf").address
On Oct 4, 2011, at 5:10 PM, recurse wrote:
> I'm having issues trying to add an event listener to a mixin class
> (which doesn't subclass Base) and wondering what kind of work-around
> there might be. It's probably easier to explain with an example
> ( http://paste.pocoo.org/show/487334/ )
>
> Basically if I call event.listen(), and pass as a target an attribute
> of a class that doesn't inherit from Base, I get an error
> "AttributeError: 'DDLEventsDispatch' object has no attribute 'set' ".
> Is there a way around this? Or a good way to automatically add a
> listener for every subclass of my Mixin?
>
> --
> 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.