File: track-modifications.rst

package info (click to toggle)
flask-sqlalchemy 3.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 832 kB
  • sloc: python: 2,909; makefile: 27; sh: 14
file content (25 lines) | stat: -rw-r--r-- 962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Tracking Modifications
======================

.. warning::
    Tracking changes adds significant overhead. In most cases, you'll be better served by
    using `SQLAlchemy events`_ directly.

.. _SQLAlchemy events: https://docs.sqlalchemy.org/core/event.html

Flask-SQLAlchemy can set up its session to track inserts, updates, and deletes for
models, then send a Blinker signal with a list of these changes either before or during
calls to ``session.flush()`` and ``session.commit()``.

To enable this feature, set :data:`.SQLALCHEMY_TRACK_MODIFICATIONS` in the Flask app
config. Then add a listener to :data:`.models_committed` (emitted after the commit) or
:data:`.before_models_committed` (emitted before the commit).

.. code-block:: python

    from flask_sqlalchemy.track_modifications import models_committed

    def get_modifications(sender: Flask, changes: list[tuple[t.Any, str]]) -> None:
        ...

    models_committed.connect(get_modifications)