File: signals.rst

package info (click to toggle)
python-django-simple-history 3.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,124 kB
  • sloc: python: 8,454; makefile: 186
file content (69 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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

For Many To Many signals you've got the following :

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

    history_instance
        The corresponding history record

    rows (for pre_create)
        The elements to be bulk inserted into the m2m table

    created_rows (for post_create)
        The created elements into the m2m table

    field
        The recorded field object

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,
        pre_create_historical_m2m_records,
        post_create_historical_m2m_records,
    )

    @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")

    @receiver(pre_create_historical_m2m_records)
    def pre_create_historical_m2m_records_callback(sender, **kwargs):
        print("Sent before saving many to many field on historical record")

    @receiver(post_create_historical_m2m_records)
    def post_create_historical_m2m_records_callback(sender, **kwargs):
        print("Sent after saving many to many field on historical record")