> For the complete documentation index, see [llms.txt](https://appsecurity.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://appsecurity.gitbook.io/devops/ppc/ppc-langs/backend/python/packages/web-frameworks/django/django-signals.md).

# Django Signals

[Сигналы](https://docs.djangoproject.com/en/5.0/topics/signals/) — очень похожая реализация паттерна Pub-Sub в Django для общения между компонентами одного приложения.

В Django есть встроенные сигналы, на которые можно подписываться (например, изменение каких-нибудь настроек), а можно создавать свои сигналы

Пример встроенного сигнала:

```python
from django.core.signals import setting_changed
```

Пример создания своего сигнала:

```python
from django.dispatch import Signal

my_signal = Signal()
```

Подписаться на сигнал (стать receiver'ом в терминах Django):

```python
from django.core.signals import setting_changed
def my_callback(sender, **kwargs):
    print("Request finished!")

setting_changed.connect(my_callback)
```

или через декоратор receiver():

```python
from django.core.signals import setting_changed
from django.dispatch import receiver


@receiver(setting_changed)
def my_callback(sender, **kwargs):
    print("Setting changed!")
```

Отправить сигнал:

```python
my_signal.send(sender=self.__class__, arg1=1, arg2="test", ...)
```

## Полный пример

Например, мы хотим подписаться на событие сохранения нашей модели Django:

```python
from django.db.models.signals import post_init, post_save
from django.dispatch import receiver

from my_model import MyModel


@receiver(post_save, sender=MyModel)
def my_receiver(sender, instance, created, **kwargs):
   # Если кто-то будет сохранять модель MyModel, то этот обработчик вызовется после обновления
   # проверяем поля instance (это будет типа MyModel), решаем что делать
   ...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://appsecurity.gitbook.io/devops/ppc/ppc-langs/backend/python/packages/web-frameworks/django/django-signals.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
