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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With