Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy flask after insert/update/delete

I am using flask and an sqlalchemy extension. Also I am using the declarative way to write my models as described in the extension's documentation.

For one of my models, I have some code I need to run after a new row has been inserted, updated or deleted. I was wondering how to do it? Ideally I would just add functions to the model..

Thanks

like image 955
applechief Avatar asked Sep 16 '12 19:09

applechief


1 Answers

Look at SQLAlchemy's Mapper Events. You can bind a callback function to the after_insert, after_update, and after_delete events.

Example:

from sqlalchemy import event

def after_insert_listener(mapper, connection, target):
    # 'target' is the inserted object
    print(target.id_user)

event.listen(User, 'after_insert', after_insert_listener)
like image 75
robots.jpg Avatar answered Sep 28 '22 08:09

robots.jpg