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

相关推荐
张张123y1 小时前
后端第九课:部署上线(把你的 FastAPI 后端真正跑起来给别人用)
python·docker·部署·fastapi
云勤通2 小时前
智慧后勤系统哪家定制能力强
大数据·python
想七想八不如114082 小时前
元学习简单入门
人工智能·pytorch·python·深度学习·学习·机器学习
一只小猪妖2 小时前
分布式唯一 性ID生成方法
后端
Zucker n2 小时前
一款适合研究生科研工作安排的日历
linux·数据库·python
小蒜学长2 小时前
微信小程序-高校自习室管理系统(代码+数据库+LW)
java·spring boot·后端·微信小程序·自习室管理
M1A12 小时前
告别代码风格之争:在 VS Code 中用 Black 一键格式化 Python
后端
IT_陈寒2 小时前
Python多进程共享变量这个坑,差点让我加班到凌晨
前端·人工智能·后端
撸串-研究生3 小时前
springboot福利院管理系统73979
java·spring boot·后端·福利院管理系统
尼给路达哟3 小时前
node(koa)和python(fastapi)面试
python·面试·fastapi