Search code examples
pythondatabaseflask-admin

Flask Admin editing columns send requests


I am using flask-admin with ModelViews

class MyModel(ModelView):
    can_create = False
    can_edit = True
    column_list = ['column']

This allows me to edit the data on each row. However I want to perform some custom function in addition to the editing. I tried to add a route for the edit but it overrides the existing functionality.

@app.route('/admin/mymodelview/edit/', methods=['POST'])
def do_something_in_addition():
    ...

Is there any way to extend the existing edit functionality?


Solution

  • Override either the after_model_change method or the on_model_change methods in your view class.

    For example :

    class MyModel(ModelView):
        can_create = False
        can_edit = True
        column_list = ['column']
    
        def after_model_change(self, form, model, is_created):
            # model has already been commited here
            # do custom work
            pass
    
        def on_model_change(self, form, model, is_created)
            # model has not been commited yet so can be changed
            # do custom work that can affect the model
            pass