简体   繁体   中英

Flask-Admin: How to change model using 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM