原创  flask-admin 数据保存后回调 修改保存

分类:python 2020-10-23T22:18:55    83人阅读   

flask-admin 中回调函数如下,详情见官方文档

def on_model_change(self, form, model, is_created):
			......
			pass


def _on_model_change(self, form, model, is_created):
			......
			pass

def after_model_change(self, form, model, is_created):
			......
			pass

实例,数据上传后添加图片水印:

class FileView(AdminView):
    def __init__(self, app, model, session, name=None, category=None, endpoint=None, url=None, static_folder=None,
                 menu_class_name=None, menu_icon_type=None, menu_icon_value=None):

        # Override form field to use Flask-Admin FileUploadField
        self.form_args = {
            'path': {
                'label': 'File',
                'base_path':  app.config['IMG_ROOT'],
                'allow_overwrite': True
            }
        }
        self.base_path = app.config['IMG_ROOT']

        super().__init__(model, session, name, category, endpoint, url, static_folder, menu_class_name,
                         menu_icon_type, menu_icon_value)

    # Override form field to use Flask-Admin FileUploadField
    form_overrides = {
        'path': form.FileUploadField
    }
#注意对齐
        def after_model_change(self, form, model, is_created):
                w = WaterMark()
                w.addWaterMark(self.base_path+'/'+model.path)

    def _list_thumbnail(view, context, model, name):
        if not model.path:
            return ''

        return Markup(
            '<a href="%s">点击查看<a><br/>' % url_for('static_img',
                                                  filename= model.path)) + 'blog2/' + model.path

    column_formatters = {
        'path': _list_thumbnail
    }


分享到: