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

相关推荐
Moment10 小时前
OpenClaw 从能聊到能干差的是这 50 个 Skills 😍😍😍
前端·后端·开源
怕浪猫11 小时前
第20章:Web服务实战——构建RESTful API
后端·go·编程语言
BingoGo11 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack11 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
摸鱼的春哥11 小时前
Agent教程14:记忆才是Agent开发的核心
前端·javascript·后端
Victor35611 小时前
MongoDB(20)如何更新MongoDB集合中的文档?
后端
Victor35611 小时前
MongoDB(21)如何删除MongoDB集合中的文档?
后端
风象南19 小时前
很多人说,AI 让技术平权了,小白也能乱杀老师傅 ?
人工智能·后端
雨中飘荡的记忆20 小时前
ElasticJob分布式调度从入门到实战
java·后端
Se7en25821 小时前
推理平台全景
后端