保存 flask-admin 编辑历史

Save flask-admin editing history

我正在尝试实现将另一列的编辑日期添加到列 'editing-date' 的方法。就像我连续更改用户名并同时在 'editing-date' 上出现类似“23.08.18”的内容。最好的方法是什么?

首先,在您的模型中将 editing_date 定义为 DateTime 列。

然后您可以在模型视图 class 定义中覆盖 on_model_change() 方法。只需将以下内容放入您的模型视图 class

from datetime import datetime
on_model_change(form, model, is_created):
    if not is_created: # True if model was created and to False if edited
                       # you only want this to run when model is edited so we are using not operator
        model.edit_date = datetime.utcnow()

on_nodel_view() 在创建或更新模型并将更改提交到数据库之前执行一些操作。每当你想在更改提交到数据库之前运行额外代码时使用它

在您的模型中定义这样一个列:

    editing_date = db.Column(db.DateTime, onupdate=datetime.utcnow)