我正在尝试使用 flask-appbuilder 构建一个用户表,并希望在此表上记录任何插入、更新、删除更改。我下面的代码扩展了 Auditmixin,以便我将有自动列来记录谁/何时创建/更新表,但我无法记录行更改历史记录以供审计。
如何在 Flask-appbuilder 中添加诸如侦听器之类的东西来触发对文件或数据库的日志记录?
模型.py
from flask_appbuilder import Model
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from flask_appbuilder.models.mixins import AuditMixin
class MyGenericModel(AuditMixin, Model):
id = Column(Integer, primary_key=True)
age = Column(Integer)
name = Column(String(100))
视图.py
from flask import render_template
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder import ModelView, ModelRestApi
from . import appbuilder, db
from app.models import MyGenericModel
class MyModelView(ModelView):
datamodel = SQLAInterface(MyGenericModel)
@appbuilder.app.errorhandler(404)
def page_not_found(e):
return (
render_template(
"404.html", base_template=appbuilder.base_template, appbuilder=appbuilder
),
404,
)
db.create_all()
appbuilder.add_view(
MyModelView,
"My View",
icon="fa-folder-open-o",
category="My Category",
category_icon='fa-envelope'
)
先感谢您。