Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi

michaelbukachi/admin.py

Last active Mar 15, 2020
Embed
What would you like to do?
Flask-Admin with `autocommit=True`
'''
This workaround helps avoid:
sqlalchemy.exc.InvalidRequestError: No transaction is begun
whenever you try to create/update a model when `autocommit=True`
'''
class BaseModelView(ModelView):
def create_model(self, form):
self.session.begin()
super(BaseModelView, self).create_model(form)
def update_model(self, form, model):
self.session.begin()
super(BaseModelView, self).update_model(form, model)
def delete_model(self, model):
self.session.begin()
super(BaseModelView, self).delete_model(model)
# Rest of the code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment