Project 4: Django Signals & Hooks (Observer Pattern)

Build an event system that mimics Django signals for decoupled side effects.


Quick Reference

Attribute Value
Difficulty Intermediate
Time Estimate 1-2 weeks
Language Python
Prerequisites OOP, decorators
Key Topics observer pattern, event bus, hooks
Output Signal library + demo usage

Learning Objectives

By completing this project, you will:

  1. Implement a Signal class with connect/send.
  2. Add sender filtering and receiver registration.
  3. Handle receiver errors safely.
  4. Define built-in signals (pre/post save).
  5. Explain when signals help and when they hurt.

The Core Question You’re Answering

“How can components react to events without tight coupling?”


Concepts You Must Understand First

Concept Why It Matters Where to Learn
Observer pattern Core design pattern GoF Design Patterns
Decorators Register receivers Python docs
Error isolation Prevent cascading failures Defensive programming

Theoretical Foundation

Sender -> Signal -> Receivers (multiple)

Signals decouple producers from consumers but can hide side effects.


Project Specification

What You Will Build

A lightweight signal system with named signals, receiver registration, and a demo that logs model save events.

Functional Requirements

  1. Create Signal.connect, Signal.disconnect, Signal.send.
  2. Support sender-based filtering.
  3. Emit pre_save and post_save signals.
  4. Collect receiver responses.
  5. Provide a decorator for registration.

Non-Functional Requirements

  • Safety: Receiver errors do not break others.
  • Clarity: Usage is documented.

Implementation Guide

Project Structure

project-root/
├── signals/
│   ├── core.py
│   └── builtins.py
└── demo.py

Questions to Guide Your Design

  1. Should receivers run in registration order?
  2. Should signals return responses?
  3. How will you log errors without silencing them?

Interview Questions

  1. What is the observer pattern?
  2. What are pros/cons of signals?
  3. How do you avoid hidden side effects?
  4. How would you make signals async?

Testing Strategy

  • Receiver runs on send.
  • Sender filtering works.
  • Exceptions isolated per receiver.

Extensions

  • Add async dispatch.
  • Add receiver priorities.

This guide was generated from LEARN_DJANGO_WEB_FRAMEWORKS.md. For the complete learning path, see the parent directory LEARN_DJANGO_WEB_FRAMEWORKS/README.md.