2

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.

Rafael
  • 1,018
  • 1
  • 10
  • 18
  • 1
    It turns out "model" is the SQLAlchemy model all along. I was missing the "self" argument in the function overload definition: `def on_model_change(self, form, model, is_created):` which caused all other arguments to be misnamed. – Rafael Jun 30 '15 at 13:26

1 Answers1

3

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))
llazzaro
  • 3,970
  • 4
  • 33
  • 47