SQLAlchemy Event Interface

I am using SQLAlchemy 0.7. I would like some “post-processing” to appear after " session.flush() ", namely, I need to access the instances participating in flush() and pass them through. A call to flush () will update the database, but the instances also contain some data in the LDAP database, so I would like SQLAlchemy to initiate the update of this LDAP database by calling the instance method.

I decided that I would use the after_flush(session, flush_context) event after_flush(session, flush_context) , in detail here , but how can I get a list of update()'d instances

On the side of the note, how can I determine which columns have been changed (or "dirty") in an instance. I was able to find out if the specimen as a whole is dirty, but not individual properties.

+4
source share
1 answer

According to the link you provided:

Note that the state of the sessions is still in pre-clearing mode, that is, the “new”, “dirty” and “deleted lists” still display the pre-clearing status as well as the instance attribute history settings.

This means that you must have access to all the dirty objects in the session.dirty list. You will notice that the first parameter of the event callback is the current session object.

As for the second part, you can use the sqlalchemy.orm.attributes.get_history function to find out which columns have changed. It returns a History object for the given attribute, which contains the has_changes() method.

If you are trying to listen for changes in certain class attributes, use Attribute Events instead.

+4
source

All Articles