Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a custom signal in Django?
A custom signal is a way to send notifications within your Django app when certain actions happen. It lets different parts of your app talk to each other without being tightly connected.
Click to reveal answer
beginner
How do you create a custom signal in Django?
You create a custom signal by importing Signal from django.dispatch and then defining a new Signal instance, like my_signal = Signal().
Click to reveal answer
beginner
What is the role of a receiver function in Django signals?
A receiver function listens for a signal and runs some code when that signal is sent. It connects to the signal using the @receiver decorator or the connect() method.
Click to reveal answer
beginner
How do you send a custom signal in Django?
You send a custom signal by calling its send() method, usually passing the sender and any extra data as keyword arguments.
Click to reveal answer
intermediate
Why use custom signals instead of calling functions directly?
Custom signals help keep your code loosely connected. This means parts of your app can work independently and be easier to maintain or change later.
Click to reveal answer
Which Django module do you import to create a custom signal?
Adjango.signals
Bdjango.dispatch
Cdjango.utils
Ddjango.models
✗ Incorrect
Custom signals are created using the Signal class from django.dispatch.
What decorator is commonly used to connect a receiver function to a signal?
A@receiver
B@signal
C@connect
D@listen
✗ Incorrect
The @receiver decorator from django.dispatch is used to connect functions to signals.
What method do you call to send a custom signal?
Asend()
Btrigger()
Cdispatch()
Demit()
✗ Incorrect
You use the send() method on the Signal instance to send a signal.
Which argument is usually passed as the sender when sending a signal?
AThe signal instance
BThe receiver function
CThe class or object sending the signal
DThe Django app name
✗ Incorrect
The sender argument identifies who is sending the signal, often a class or object.
What is a key benefit of using custom signals in Django?
AMakes code run faster
BReplaces the need for views
CTightens code coupling
DAllows parts of code to communicate without direct calls
✗ Incorrect
Custom signals help different parts of your app communicate without being tightly connected.
Explain how to create, connect, and send a custom signal in Django.
Think about the steps from defining the signal to making it work.
You got /5 concepts.
Describe why using custom signals can improve your Django app's design.
Consider how signals affect code connections.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of custom signals in Django?
easy
A. To create new database tables dynamically
B. To speed up database queries automatically
C. To replace Django's URL routing system
D. To allow different parts of an app to communicate without being tightly connected
Solution
Step 1: Understand what custom signals do
Custom signals let different parts of a Django app send messages to each other without direct links.
Step 2: Compare options to this purpose
Only To allow different parts of an app to communicate without being tightly connected describes this communication purpose; others describe unrelated features.
Final Answer:
To allow different parts of an app to communicate without being tightly connected -> Option D
Quick Check:
Custom signals = loose communication [OK]
Hint: Custom signals help parts talk without tight links [OK]
Common Mistakes:
Thinking signals speed up queries
Confusing signals with URL routing
Believing signals create database tables
2. Which of the following is the correct way to define a custom signal in Django?
easy
A. my_signal = signal(["instance", "created"])
B. my_signal = Signal(providing_args=["instance", "created"])
C. my_signal = Signal(args=["instance", "created"])
D. my_signal = Signal(provides=["instance", "created"])
Solution
Step 1: Recall Django's Signal class syntax
The correct way is to create a Signal object with providing_args as a list of argument names.
Step 2: Check each option's syntax
Only my_signal = Signal(providing_args=["instance", "created"]) uses Signal with providing_args correctly; others use wrong parameter names or lowercase Signal.
Final Answer:
my_signal = Signal(providing_args=["instance", "created"]) -> Option B
Quick Check:
Signal(providing_args=...) is correct syntax [OK]
Hint: Use Signal(providing_args=[...]) to define custom signals [OK]
Common Mistakes:
Using lowercase 'signal' instead of 'Signal'
Using 'args' or 'provides' instead of 'providing_args'
Passing arguments without a list
3. Given this code snippet, what will be printed when my_signal.send(sender=None, instance='obj1', created=True) is called?
The signal is sent with instance='obj1' and created=True. The receiver prints these values from kwargs.
Step 2: Match printed output to sent values
The print statement uses kwargs['instance'] and kwargs['created'], so it prints 'obj1' and 'True'.
Final Answer:
Received: obj1, Created: True -> Option A
Quick Check:
Signal send values print correctly [OK]
Hint: Receiver prints kwargs values sent by signal [OK]
Common Mistakes:
Confusing sender with instance
Assuming created is False by default
Thinking sender is required in print
4. What is wrong with this code that tries to connect a receiver to a custom signal?
from django.dispatch import Signal
my_signal = Signal(providing_args=["data"])
def receiver_func(sender, data):
print(f"Data: {data}")
my_signal.connect(receiver_func)
medium
A. The receiver function must accept **kwargs, not just named arguments
B. Signal must be imported from django.signals, not django.dispatch
C. The connect method requires a sender argument
D. providing_args should be a tuple, not a list
Solution
Step 1: Check receiver function signature
Receivers must accept sender and **kwargs to handle all signal arguments flexibly.
Step 2: Identify mismatch in receiver parameters
The receiver only accepts sender and data, missing **kwargs, which causes errors when extra arguments are sent.
Final Answer:
The receiver function must accept **kwargs, not just named arguments -> Option A
Quick Check:
Receiver needs **kwargs for signal args [OK]
Hint: Receiver functions always need **kwargs parameter [OK]
Common Mistakes:
Forgetting **kwargs in receiver signature
Importing Signal from wrong module
Assuming connect requires sender argument
Confusing list and tuple for providing_args
5. You want to create a custom signal that notifies when a user profile is updated, sending the user instance and a flag if the update was major. Which of these is the best way to define and send this signal?
hard
A. Define signal with no providing_args and send with user=user_obj, major_update=True
B. Define signal with providing_args=['user'] and send with user=user_obj, major_update=True
C. Define signal with providing_args=['user', 'major_update'] and send with user=user_obj, major_update=True
D. Define signal with providing_args=['user', 'major_update'] but send only user=user_obj
Solution
Step 1: Define signal with all expected arguments
Since you want to send both user and major_update, both must be listed in providing_args.
Step 2: Send signal with matching arguments
When sending, include both user and major_update to match the signal definition and receiver expectations.
Final Answer:
Define signal with providing_args=['user', 'major_update'] and send with user=user_obj, major_update=True -> Option C
Quick Check:
Signal args must match send args [OK]
Hint: Match providing_args and send arguments exactly [OK]
Common Mistakes:
Omitting arguments in providing_args
Sending arguments not declared in providing_args
Defining signal without providing_args but sending args