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

相关推荐
牛马的人生12 分钟前
使用亮数据代理IP+Python爬虫批量爬取招聘信息训练面试类AI智能体(手把手教学版)
爬虫·python·tcp/ip·其他
leo_hush33 分钟前
python查询elasticsearch 获取指定字段的值的list
python·elasticsearch
红衣小蛇妖1 小时前
Python基础学习-Day30
开发语言·python·学习
源码云商1 小时前
基于Spring Boot + Vue的教师工作量管理系统设计与实现
vue.js·spring boot·后端
CodeCraft Studio1 小时前
PDF处理控件Aspose.PDF教程:以编程方式将 PDF 导出为 JPG
java·python·pdf·.net
于归pro1 小时前
Python环境管理工具深度指南:pip、Poetry、uv、Conda
python·pip·uv
拾零吖2 小时前
Python基础
python
大模型铲屎官2 小时前
【Python-Day 16】代码复用基石:详解 Python 函数的定义与调用
开发语言·人工智能·pytorch·python·深度学习·大模型·函数
从零开始学习人工智能2 小时前
深入解析异步编程:Java NIO、Python `async/await` 与 C# `async/await` 的对比
java·python·nio
love530love2 小时前
【笔记】PyCharm 中创建Poetry解释器
运维·人工智能·windows·笔记·python·pycharm·conda