Issue
I am using Flask and a 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.
Solution
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)
Answered By - robots.jpg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.