On Tue, Oct 14, 2014 at 12:53 PM, Eugene Prikazchikov
<eprik...@gmail.com> wrote:
> I am struggling to understand how to use session events. I want to track
> inserts&updates in some model and depending on certain criteria modify
> objects from other model. Judging from docs, I cannot use before_update and
> similar Mapper Events:
> http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=before_flush#sqlalchemy.orm.events.MapperEvents.before_update
> And from another place in that doc: "The user is encouraged to evaluate the
> SessionEvents.before_flush() and SessionEvents.after_flush() methods as more
> flexible and user-friendly hooks in which to apply additional database state
> during a flush"
>
> However, docs for session events are very scarce. Are there any examples for
> using before_flush/after_flush events? For example in case of flush with
> single update operation, how can I access the object that was updated?
>

The before_flush handler receives the session as its first parameter.
You can then iterate over the session.dirty, session.new and
session.deleted properties as necessary to find the objects you are
interested in.

The "versioning" example uses this to put rows into a history table
whenever an object changes:

  
https://bitbucket.org/zzzeek/sqlalchemy/src/09e2a15a8052/examples/versioned_history/history_meta.py?at=master#cl-204

Hope that helps,

Simon

-- 
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/d/optout.

Reply via email to