Flask Admin编辑列发送请求

时间:2016-08-02 02:42:06

标签: python database flask-admin

我正在使用带有ModelViews的flask-admin

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

这允许我编辑每一行的数据。但是我想在编辑之外执行一些自定义功能。我尝试为编辑添加路由,但它会覆盖现有功能。

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

有没有办法扩展现有的编辑功能?

1 个答案:

答案 0 :(得分:1)

覆盖视图类中的after_model_change方法或on_model_change方法。

例如:

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