Flask-Admin:如何使用 on_model_change 更改模型? [英] Flask-Admin: How to change model using on_model_change?

查看:62
本文介绍了Flask-Admin:如何使用 on_model_change 更改模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字段的值设置为基于同一表单中的另一个字段:

I'm trying to set a field's value to be based on another field in the same form:

    def on_model_change(form, model, is_created):
        model.textcolumn.data = model.textcolumn2.data

通过 Flask-Admin 界面更新不会引发任何异常,但没有对 model.textcolumn 中的值进行任何更改.

Updating via the Flask-Admin interface raises no exceptions, but no changes were made to the value in model.textcolumn.

检查模型"对象,我还注意到这与用于生成 ModelView 的 SQLAlchemy 模型不同.

Inspecting the "model" object, I also noticed this is not the same as the SQLAlchemy model used to generate the ModelView.

如何将model.textcolumn 的值更改为model.textcolumn2 的值?有没有办法直接访问 SQLAlchemy 模型对象?这样会好很多.

How can I change model.textcolumn's value to model.textcolumn2's value? Is there a way to access the SQLAlchemy model object directly? This would be much better.

推荐答案

我使用特定模型的管理表单做到了

I did it using the admin form for a specific model

class YourModelAdmin(MyModelView):
    form_overrides = dict(filename=FileField)

    def on_model_change(self, form, instance):
        pass

   admin.add_view(YourModelAdmin(YourModel, db.session))

这篇关于Flask-Admin:如何使用 on_model_change 更改模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆