Flask-Admin: How to change model using on_model_change?

雨燕双飞 提交于 2019-12-10 10:48:45

问题


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

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

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

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.


回答1:


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))


来源:https://stackoverflow.com/questions/31127156/flask-admin-how-to-change-model-using-on-model-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!