flask-admin 在modelview 视图中重写on_model_change 与after_model_change

背景:

当我们在使用flask-admin进行WEB开发时应该第一时间想到的是竟可能使用框架推荐的modelView模型,其次才是自定义模型 baseview,因为只有modelview模型下开发才能最大限度的提高效率。

制作:

1、在modelview视图下框架会通过默认视图进行展示模型信息,有时候因业务需要在模型数据增改时需要做一些前置或者后置操作。

2、框架提供on_model_change和 after_model_change2个函数来分别应模型数据修改前和修改后事件

复制代码
    def after_model_change(self, form, model, is_created):
        """
            Perform some actions after a model was created or updated and
            committed to the database.

            Called from create_model after successful database commit.

            By default does nothing.

            :param form:
                Form used to create/update model
            :param model:
                Model that was created/updated
            :param is_created:
                True if model was created, False if model was updated
        """
        pass

    def on_model_change(self, form, model, is_created):
        """
            Perform some actions before a model is created or updated.

            Called from create_model and update_model in the same transaction
            (if it has any meaning for a store backend).

            By default does nothing.

            :param form:
                Form used to create/update model
            :param model:
                Model that will be created/updated
            :param is_created:
                Will be set to True if model was created and to False if edited
        """
        pass

注意:a、2个函数的model中都可以拿到当前模型兑换ing表单form 信息和DB模型信息

b、这2个函数只能在modelview 模型及其子模型中生效

相关推荐
青梅主码2 分钟前
Artificial Analysis 刚刚重磅发布《2025 年第三季度人工智能亮点》报告:中国仅落后美国几个月(附下载)
后端
我的xiaodoujiao7 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest
格格步入7 分钟前
🤔一次 OOM 排查(dump文件分析)
java·后端
别慌,让我先缓缓11 分钟前
PyModbus 从入门到精通教程
python
nppe612 分钟前
NestJs 从入门到实战项目笔记
前端·后端
蓝-萧14 分钟前
Spring Security安全框架原理与实战
java·后端
景彡先生20 分钟前
Python Flask详解:从入门到实战,轻量级Web框架的魅力
前端·python·flask
SimonKing41 分钟前
聊聊Spring里那个不打扰Controller就能统一改响应的“神器”
java·后端·程序员
Moment1 小时前
Soul 发布超强端侧语音模型,没错,就是你想的那个 Soul 😍😍😍
前端·后端·github
鬼火儿1 小时前
Redis Desktop Manager(Redis可视化工具)安装
java·后端