pallets-eco/flask-admin

Intercepting and executing custom code on object creation/update in flask-admin

jalvarezz13 opened this issue · 2 comments

Hi all!

Is there a way in Flask-Admin to intercept and execute custom code before or after a create or update operation on an object within the database?

I've been exploring SQLAlchemy events and Flask-Admin functions, but I haven't found a direct way to achieve this.
Does anyone have experience handling these specific events in Flask-Admin and could provide guidance or examples on how to implement it?

Thanks in advance!

Hi.

I was able to achieve the result you describe with @listens_for event listener decorator.

@listens_for(YourModel, 'after_insert')
def create_something_method(mapper, connection, current_model):
    # Your code

@listens_for(YourModel, 'after_update')
def update_something_method(mapper, connection, current_model):
    # Your code
 

More information can be found here https://docs.sqlalchemy.org/en/20/core/event.html

Hope this helps you and anyone looking for this kind of behavior.

I finally reached my goal using the native on_model_change and on_model_delete handlers ;)
I will close the issue, then. Thanks!

image