问题
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