21. Signals

21.1. Available signals

21.1.1. Model signals

  • django.db.models.signals.pre_init

  • django.db.models.signals.post_init

  • django.db.models.signals.pre_save

  • django.db.models.signals.post_save

  • django.db.models.signals.pre_delete

  • django.db.models.signals.post_delete

21.1.2. M2m relations

  • django.db.models.signals.m2m_changed

  • django.db.models.signals.class_prepared

21.1.3. Migrations

  • django.db.models.signals.pre_migrate

  • django.db.models.signals.post_migrate

21.1.4. Request

  • django.core.signals.request_started

  • django.core.signals.request_finished

  • django.core.signals.got_request_exception

21.1.5. Test

  • django.test.signals.setting_changed

  • django.test.signals.template_rendered

21.1.6. Database

  • django.db.backends.signals.connection_created

21.2. Examples

from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import MyModel


@receiver(pre_save, sender=MyModel)
def my_handler(sender, **kwargs):
    ...
from django.apps import AppConfig
from django.db.models.signals import post_migrate

def my_callback(sender, **kwargs):
    # Your specific logic here
    pass

class MyAppConfig(AppConfig):
    ...

    def ready(self):
        post_migrate.connect(my_callback, sender=self)