Signal dispatch process
📖 Scenario: You are building a Django app that needs to perform an action automatically when a new user is created.We will use Django signals to detect when a user is saved and then run custom code.
🎯 Goal: Create a Django signal handler that listens for the post_save signal on the User model and prints a message when a new user is created.
📋 What You'll Learn
Import the
post_save signal from django.db.models.signalsImport the
User model from django.contrib.auth.modelsDefine a function called
welcome_new_user that accepts sender, instance, created, and **kwargs parametersConnect the
welcome_new_user function to the post_save signal for the User modelInside the signal handler, check if
created is True and then print "Welcome, {username}!" where {username} is the username of the new user💡 Why This Matters
🌍 Real World
Automatically perform actions like sending welcome messages or updating related data when database changes happen.
💼 Career
Understanding Django signals is important for backend developers to write clean, decoupled code that reacts to model events.
Progress0 / 4 steps