I am using SQLAlchemy, and face a problem, that I need to handle event, when one field in table is changed. Firstly, I've tried to use code from docs:
@listen_for(Advert.status, 'set')
def handle_change_status(target, value, oldvalue, initiator):
if value != oldvalue:
# do smth
But this event occure any time the field is changed, even if we do not modify db. For example:
advert=Advert()
advert.status = 3 # event apear
But I need that event appear only when I commit changes:
db.session.add(advert)
db.session.commit() # I need event occure here
I think that I can handle each 'set' event and write them in some storage, and then add event on session.commit (after_commit) and in this event handle all set events. But this approach seems not to be very neat.
May be someone has any ideas, how to solve this problem?
@event.listens_for(SomeClass, 'after_insert')could help here? Version 1.3