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

相关推荐
DROm RAPS5 分钟前
十七:Spring Boot依赖 (2)-- spring-boot-starter-web 依赖详解
前端·spring boot·后端
Bert.Cai5 分钟前
Python字面量详解
开发语言·python
Flying pigs~~8 分钟前
基于Deepseek大模型API完成文本分类预测功能
java·前端·人工智能·python·langchain·deepseek
Ujimatsu33 分钟前
数据分析相关面试题-Python部分
大数据·python·数据分析
未知鱼34 分钟前
Python安全开发之简易Xss检测工具(详细注释)
python·安全·xss
yaoxin52112342 分钟前
368. Java IO API - 基本文件属性
java·开发语言·python
程序媛徐师姐1 小时前
Python基于机器学习的就业岗位推荐系统【附源码、文档说明】
python·机器学习·python机器学习·就业岗位推荐系统·python就业岗位推荐系统·python机器学习就业推荐·就业岗位推荐
建军啊1 小时前
java审计进阶
java·开发语言·python
TlYf NTLE1 小时前
Spring Boot spring-boot-maven-plugin 参数配置详解
spring boot·后端·maven