| 1 |
Tracking Modifications |
| 2 |
====================== |
| 3 |
|
| 4 |
.. warning:: |
| 5 |
Tracking changes adds significant overhead. In most cases, you'll be better served by |
| 6 |
using `SQLAlchemy events`_ directly. |
| 7 |
|
| 8 |
.. _SQLAlchemy events: https://docs.sqlalchemy.org/core/event.html |
| 9 |
|
| 10 |
Flask-SQLAlchemy can set up its session to track inserts, updates, and deletes for |
| 11 |
models, then send a Blinker signal with a list of these changes either before or during |
| 12 |
calls to ``session.flush()`` and ``session.commit()``. |
| 13 |
|
| 14 |
To enable this feature, set :data:`.SQLALCHEMY_TRACK_MODIFICATIONS` in the Flask app |
| 15 |
config. Then add a listener to :data:`.models_committed` (emitted after the commit) or |
| 16 |
:data:`.before_models_committed` (emitted before the commit). |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
from flask_sqlalchemy.track_modifications import models_committed |
| 21 |
|
| 22 |
def get_modifications(sender: Flask, changes: list[tuple[t.Any, str]]) -> None: |
| 23 |
... |
| 24 |
|
| 25 |
models_committed.connect(get_modifications) |