ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Mastering Django Signals

author
Generated by
Nidhi Singh

26/10/2024

django

Sign in to read full article

Introduction to Django Signals

Django signals are a powerful feature that allow decoupled applications to get notified when certain actions occur elsewhere in the framework. They provide a way to allow certain senders to notify a set of receivers that some action has taken place.

Think of signals as a way for different parts of your application to communicate with each other without being directly dependent on one another. This can lead to cleaner, more maintainable code.

Built-in Django Signals

Django comes with a variety of built-in signals that cover common scenarios. Some of the most frequently used signals include:

  1. pre_save and post_save: Sent before and after a model's save() method is called.
  2. pre_delete and post_delete: Sent before and after a model's delete() method is called.
  3. request_started and request_finished: Sent when Django starts or finishes processing an HTTP request.

Let's look at a simple example of how to use a built-in signal:

from django.db.models.signals import post_save from django.dispatch import receiver from django.contrib.auth.models import User from .models import Profile @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save()

In this example, we're using the post_save signal to automatically create and save a user profile whenever a new user is created.

Creating Custom Signals

While built-in signals are great for many common scenarios, you might find yourself needing to create custom signals for more specific use cases. Here's how you can create and use custom signals:

  1. Define your custom signal:
# signals.py from django.dispatch import Signal order_completed = Signal()
  1. Send the signal when appropriate:
# views.py from .signals import order_completed def complete_order(request): # Process the order # ... order_completed.send(sender=self.__class__, order=order)
  1. Connect a receiver to the signal:
# receivers.py from django.dispatch import receiver from .signals import order_completed @receiver(order_completed) def notify_customer(sender, order, **kwargs): # Send an email to the customer # ...

Advanced Signal Usage

Passing Custom Arguments

You can pass custom arguments when sending a signal:

order_completed.send(sender=self.__class__, order=order, special_offer=True)

And receive them in your receiver:

@receiver(order_completed) def process_special_offer(sender, order, special_offer=False, **kwargs): if special_offer: # Apply special offer logic # ...

Connecting Without Decorators

If you prefer not to use decorators, you can connect signals manually:

from django.core.signals import request_finished from django.dispatch import receiver def my_callback(sender, **kwargs): print("Request finished!") request_finished.connect(my_callback)

Disconnecting Signals

Sometimes you might need to disconnect a signal, especially in testing scenarios:

from django.core.signals import request_finished request_finished.disconnect(my_callback)

Best Practices and Considerations

  1. Keep signal receivers small and focused. If you need complex logic, consider moving it to a separate function.

  2. Be mindful of performance. Signals are synchronous by default, which means they can slow down your application if overused.

  3. Use signals for cross-cutting concerns that don't fit well within your main application logic.

  4. Document your custom signals well, especially if they're part of a reusable app.

  5. Be careful with circular imports when working with signals. It's often best to define signals in a separate signals.py file.

Conclusion

Django signals offer a powerful way to decouple your application logic and respond to events across your project. By understanding both built-in and custom signals, you can create more flexible and maintainable Django applications.

As you continue your journey in Django, experiment with different signal patterns and see how they can improve your code structure. Remember, the key is to use signals judiciously – they're a tool in your toolkit, not a solution for every problem.

Popular tags

djangopythonsignals

Share now!

Like & bookmark

Related collections

  • Mastering Computer Vision with OpenCV

    06/12/2024 · Python

  • Streamlit Mastery: From Basics to Advanced

    15/11/2024 · Python

  • Mastering Hugging Face Transformers

    14/11/2024 · Python

  • PyTorch Mastery: From Basics to Advanced

    14/11/2024 · Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 · Python

Related articles

  • Understanding Streamlit Architecture

    15/11/2024 · Python

  • Leveraging Python for Efficient Structured Data Processing with LlamaIndex

    05/11/2024 · Python

  • Mastering Background Tasks and Scheduling in FastAPI

    15/10/2024 · Python

  • Seaborn Fundamentals

    06/10/2024 · Python

  • Streamlining Your Workflow

    14/11/2024 · Python

  • Mastering Numerical Computing with NumPy

    25/09/2024 · Python

  • Optimizing Performance in Streamlit Apps

    15/11/2024 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design