File: signals.rst

package info (click to toggle)
python-django-simple-history 2.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 732 kB
  • sloc: python: 4,273; makefile: 187
file content (41 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (3)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Signals
------------------------------------
`django-simple-history` includes signals that help you provide custom behavior when
saving a historical record. Arguments passed to the signals include the following:

.. glossary::
    instance
        The source model instance being saved

    history_instance
        The corresponding history record

    history_date
        Datetime of the history record's creation

    history_change_reason
        Freetext description of the reason for the change

    history_user
        The user that instigated the change

    using
        The database alias being used

To connect the signals to your callbacks, you can use the ``@receiver`` decorator:

.. code-block:: python

    from django.dispatch import receiver
    from simple_history.signals import (
        pre_create_historical_record,
        post_create_historical_record
    )

    @receiver(pre_create_historical_record)
    def pre_create_historical_record_callback(sender, **kwargs):
        print("Sent before saving historical record")

    @receiver(post_create_historical_record)
    def post_create_historical_record_callback(sender, **kwargs):
        print("Sent after saving historical record")