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:
- Implement a Signal class with connect/send.
- Add sender filtering and receiver registration.
- Handle receiver errors safely.
- Define built-in signals (pre/post save).
- 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
- Create
Signal.connect,Signal.disconnect,Signal.send. - Support sender-based filtering.
- Emit
pre_saveandpost_savesignals. - Collect receiver responses.
- 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
- Should receivers run in registration order?
- Should signals return responses?
- How will you log errors without silencing them?
Interview Questions
- What is the observer pattern?
- What are pros/cons of signals?
- How do you avoid hidden side effects?
- 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.