从编辑视图自定义(覆盖)Flask-Admin的提交方法 [英] Customize (override) Flask-Admin's Submit method from edit view

查看:1443
本文介绍了从编辑视图自定义(覆盖)Flask-Admin的提交方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前提条件:

我是Python新手,特别是Flask-Admin。我创建了一个简单的测试服务,它有MondoDB,使数据保持一对一的关系。


employeeName - >工资


模型看起来像这样:
$ b $ pre $ class $ Employee $(db.Document)
fullName = db.StringField(max_length = 160 ,unique = True)
salary = db.IntField()

我使用Flask-管理员用数据观察表格并对其进行编辑。
当我想更改工资字段时,只需按下编辑按钮,并在Flask-Admin的默认编辑视图中更改整数值。我按'提交',并在数据库中的新值成功应用。



问题:

但我需要以某种方式覆盖提交方法,即保留功能,并添加一些自定义代码。我们假设我想在实际的db提交之后在日志文件中添加注释:

lockquote

logging.warning(' s:被更改为/%s',fullName,薪水)

有关如何实现这一点的任何建议将不胜感激。也许你可以指引我走,因为Flask-Admin文档到目前为止还没有给我足够的帮助。

解决方案

<你可以重载 on_model_change 方法来添加你的自定义逻辑。检查 http://flask-admin.readthedocs。 org / en / latest / api / mod_model /#flask.ext.admin.model.BaseModelView.on_model_change


Preconditions:
I'm new to Python and to Flask-Admin in particular. I created a simple test service, which has MondoDB, keeping the data with relationship of 'one-to-one' kind.

employeeName -> salary

The model looks like that:

class Employee(db.Document):
    fullName = db.StringField(max_length=160, unique=True)
    salary = db.IntField()

And I use Flask-Admin to observe the table with the data and to edit it. When I want to change the 'salary' field, I just press the 'edit' button and in Flask-Admin's default edit view I change the integer value. I press 'Submit' and a new value in the database is successfully applied.

Question:
But I need to override the Submit method in the way, that leaves as it is the functionality and adds some custom code. Like let's assume I want to add a comment in the log file after an actual db submit:

logging.warning('The salary of %s: was changed to /%s', fullName, salary)

Any suggestion on how to achieve that would be much appreciated. Perhaps you could direct me in the way to go, since the Flask-Admin documentation doesn't give me enough help so far.

解决方案

You can override on_model_change method to add your custom logic. Check http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.on_model_change

这篇关于从编辑视图自定义(覆盖)Flask-Admin的提交方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆