The problem:
The data is never refreshed in the UI when modified by something else than the UI.
Steps to reproduce:
Proposed solution is to expunge the objects after we load them to the session, or open/close the session every time we refresh the page although its open for discussion.
If you change data with psql - do you commit it? What is your transaction isolation level? Do you use scoped session (and close it at the end of each request as suggested by SQLAlchemy docs)?
Also, see if this might help you: https://github.com/flask-admin/flask-admin/issues/1166
Thank you for the prompt response @mrjoes.
When I edit the data, I commit the session. I do use scoped session with flask-admin, I have tried to use the isolation_level='READ COMMITTED' as suggested by @busturdust in #1166 but unfortunately it did not help.
Do you close scoped session after each request?
This is required and Flask-SQLAlchemy automatically does it. If you use vanilla SQLAlchemy, you will have to do it yourself. Failing to close the session will keep all tracked models in the memory and will cause the memory leaks.
Here's how it should look like:
@app.teardown_request
def app_teardown(response_or_exc):
# Assuming that `session` is your scoped session
session.remove()
return response_or_exc
Also see here: http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/
Although I'm not using Flask sqlalchemy, it seems removing the session in the @app.teardown_request does help.
Thank you so much for your help @mrjoes
I got similar problems but after changing data from admin panel (especially for columns with relationships).
You could see it (after changes was made in the view) in following way:
Technologies used for my case: SqlAlchemy (not Flask version), Gunicorn with few workers and Nginx
Also, it could not be reproduced locally, only on the server with production environment
So the solution for my case was to add:
@app.teardown_appcontext # the same as @app.teardown_request but for Flask >=0.9
def shutdown_session(exception=None):
# load all expired attributes for the given instance
session.expire_all()
If I'm missing smth, please let me know
And here you could find some useful info
@app.teardown_request
def app_teardown(response_or_exc):
# Assuming that `session` is your scoped session
session.remove()
return response_or_exc
This helped @mrjoes. I was having inconsistent results in my Flask-Admin. After I moved my code to production.
Most helpful comment
I got similar problems but after changing data from admin panel (especially for columns with relationships).
You could see it (after changes was made in the view) in following way:
Technologies used for my case: SqlAlchemy (not Flask version), Gunicorn with few workers and Nginx
Also, it could not be reproduced locally, only on the server with production environment
So the solution for my case was to add:
If I'm missing smth, please let me know
And here you could find some useful info