Flask Admin 编辑列发送请求

Flask Admin editing columns send requests

我正在将 flask-admin 与 ModelViews 一起使用

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

这让我可以编辑每一行的数据。但是,除了编辑之外,我还想执行一些自定义功能。我试图为编辑添加一条路线,但它覆盖了现有功能。

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

有什么方法可以扩展现有的编辑功能吗?

覆盖您视图中的 after_model_change method or the on_model_change 方法 class。

例如:

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