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

相关推荐
一晌小贪欢5 分钟前
Python 爬虫进阶:如何利用反射机制破解常见反爬策略
开发语言·爬虫·python·python爬虫·数据爬虫·爬虫python
Java编程爱好者5 分钟前
2026版Java面试八股文总结(春招+秋招+社招),建议收藏。
后端
躺平大鹅14 分钟前
5个实用Python小脚本,新手也能轻松实现(附完整代码)
python
yukai0800820 分钟前
【最后203篇系列】039 JWT使用
python
朱昆鹏20 分钟前
开源 Claude Code + Codex + 面板 的未来vibecoding平台
前端·后端·github
REDcker23 分钟前
gRPC开发者快速入门
服务器·c++·后端·grpc
figo10tf23 分钟前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
zhangyi_viva26 分钟前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
程序员敲代码吗37 分钟前
Spring Boot与Tomcat整合的内部机制与优化
spring boot·后端·tomcat
独好紫罗兰1 小时前
对python的再认识-基于数据结构进行-a006-元组-拓展
开发语言·数据结构·python