</>
Skip to content
Django lessons (35/43)

Django — Signals

What are Signals?

Signals allow certain senders to notify a set of receivers when some action has taken place.

Built-in Signals

SignalSenderDescription
pre_saveModelBefore save
post_saveModelAfter save
pre_deleteModelBefore delete
post_deleteModelAfter delete
m2m_changedManyToManyM2M field changed

Basic Signal

# blog/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

Connect Signals

# blog/apps.py
class BlogConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'blog'
    
    def ready(self):
        import blog.signals

Custom Signals

from django.dispatch import Signal

order_completed = Signal()

# Send signal
order_completed.send(sender=Order, order=order)

# Receive signal
@receiver(order_completed)
def send_confirmation(sender, order, **kwargs):
    send_mail('Order confirmed', 'Your order is ready', ...)

Mini Practice

  1. Create a post_save signal
  2. Connect signals in apps.py
  3. Create a custom signal
  4. Use signals for auditing

Up Next

Continue with Testing — writing tests in Django.

Related Topics

Frequently Asked Questions about Signals

What is Signals in Django?

Signals is a fundamental concept in Django. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Signals?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Signals.

Why is Signals important in Django?

Signals is essential for Django development. Understanding this concept will help you write better code and solve real-world problems more effectively.