0
0
Djangoframework~20 mins

pre_save and post_save signals in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Signal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a pre_save signal modifies a model field?
Consider a Django model with a pre_save signal that changes a field value before saving. What will be the final saved value in the database?
Django
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)

@receiver(pre_save, sender=Product)
def update_price(sender, instance, **kwargs):
    if instance.price < 10:
        instance.price = 10.00

p = Product(name='Pen', price=5.00)
p.save()
print(Product.objects.get(name='Pen').price)
A10.00
B5.00
C0.00
DRaises an error
Attempts:
2 left
💡 Hint
Think about when pre_save runs and what it can change.
state_output
intermediate
2:00remaining
What is printed by this post_save signal?
Given a post_save signal that prints a message after saving a model, what will be printed when a new instance is created?
Django
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

class UserProfile(models.Model):
    username = models.CharField(max_length=50)

@receiver(post_save, sender=UserProfile)
def notify_save(sender, instance, created, **kwargs):
    if created:
        print(f"Created user: {instance.username}")
    else:
        print(f"Updated user: {instance.username}")

u = UserProfile(username='alice')
u.save()
ARaises an error
BCreated user: alice
CNo output
DUpdated user: alice
Attempts:
2 left
💡 Hint
Check the created flag in post_save.
📝 Syntax
advanced
2:00remaining
Which option correctly connects a pre_save signal to a model?
Select the code snippet that correctly registers a pre_save signal handler for a Django model named Order.
A
from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save, sender=Order)
def before_save(sender, instance, **kwargs):
    print('Saving order')
B
from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(post_save, sender=Order)
def before_save(sender, instance, **kwargs):
    print('Saving order')
C
from django.db.models.signals import pre_save

@pre_save.connect

def before_save(sender, instance):
    print('Saving order')
D
from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save)
def before_save(sender, instance, **kwargs):
    print('Saving order')
Attempts:
2 left
💡 Hint
Remember to specify the sender model in the @receiver decorator.
🔧 Debug
advanced
2:00remaining
Why does this post_save signal cause a recursion error?
Analyze the code below. Why does saving the instance inside the post_save signal cause a recursion error?
Django
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    bio = models.TextField(blank=True)

@receiver(post_save, sender=Profile)
def update_bio(sender, instance, **kwargs):
    if not instance.bio:
        instance.bio = 'Default bio'
        instance.save()

p = Profile()
p.save()
ABecause the signal handler is missing the <code>created</code> argument.
BBecause <code>post_save</code> signals cannot modify model instances.
CBecause calling <code>instance.save()</code> inside <code>post_save</code> triggers <code>post_save</code> again, causing infinite recursion.
DBecause <code>bio</code> field cannot be blank, causing a validation error.
Attempts:
2 left
💡 Hint
Think about what happens when you save inside a post_save signal.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the difference between pre_save and post_save signals?
Choose the most accurate description of how pre_save and post_save signals behave in Django.
A<code>pre_save</code> only runs when creating new instances; <code>post_save</code> only runs when updating existing instances.
B<code>pre_save</code> runs after saving the instance; <code>post_save</code> runs before saving and can modify fields.
C<code>pre_save</code> and <code>post_save</code> both run after saving but differ in the order of execution.
D<code>pre_save</code> runs before the model instance is saved and can modify fields; <code>post_save</code> runs after saving and can access the saved instance but changes won't affect the current save.
Attempts:
2 left
💡 Hint
Consider when each signal is triggered relative to the database save operation.