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 模型及其子模型中生效

相关推荐
共享家95271 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr2 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢2 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模2 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn2 小时前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr3 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448913 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu3 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472463 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤3 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板