Search code examples
python-2.7flaskflask-admin

How change button logic on Flask Admin?


I want to change logic of button on Flask-Admin. Exactly: Save button. Save button should send request on other URL and then save response on Data Base. Can i do this? How?


Solution

  • If you create a class that extends flask.ext.admin.contrib.sqla.ModelView, you can override the on_model_change method.

    See http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.on_model_change

    For example, something like this:

    from flask.ext.admin.contrib.sqla import ModelView
    
    from my_app import app, db
    from models import MyModel
    
    
    class MyModelView(ModelView):
        ...
    
        def on_model_change(self, form, model, is_created):
            # Custom code to call URL
            # Custom code to save to DB
            ...
    
    admin = Admin(app)
    admin.add_view(MyModelViewView(MyModel, db.session))
    

    You might also find your question has been answered here: Customize (override) Flask-Admin's Submit method from edit view