• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Django Signals Example with Code

Aniruddha Chaudhari/405/0
Django

In this tutorial, we are going to learn…

  • What is Django Signals and its importance?
  • How does Django Signals work? (Real demo)
  • Writing and understanding Django Signals code.
  • Django Signals usage (Use cases)
  • Django Signals Event (post_save, pre_save, post_delete, pre_delete)

You can watch the below video where I have explained all the points about Django Signals along with a demo. Or you can also keep continuing reading.

Do you want to call some function or do you want to perform some action after performing a Django event like submitting data into the Django Model?

You need a Django signal.

It is easy to achieve that with just a few lines of code.

How does Django Signals Work?

You have to import two Python objects- called post_save and receiver which are available in the Django signal and Django dispatch modules. These modules come preinstalled with Django, so you don’t have to install any Django or Python package.

Here we are calling the function data_submmited every time when data is saved in the Django model.

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save)
def data_submitted(sender, instance, **kwargs):
  # Do your task.
  print("Form submiited")

If you look into the above code, we are using the decorator @receiver() along with the post_save event.

Function data_submiited() will be called for all the model data submission. This function is called with multiple arguments like sender (model name), instance (instance of the model), and some others like signal, created, update_fields, raw and using. You can just print kwargs map data to check arguements as key-value pairs.

What if you want to call the function only after submitting specific model data?

It’s very much possible.

Assign that model name to the parameter sender and pass it to the decorator @receiver() as below.

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=MyModel)
def data_submitted(sender, instance, **kwargs):
  # Do your task.
  print("MyModel data submiited")

Now, the function data_submiited will be called only after the model MyModel data submission.

Note: You can define these functions in the model.py. Or you can even write a separate file to capture all the Django signal events.

Django Signals Usage

Let me give you an example. This will make it easy to understand.

I was working on the online CV builder which is developed using the Django framework. It includes multiple model forms for personal data, employment data, academic data, etc. I wanted to maintain the timestamp as last time when the CV is updated.

To add this feature, I’m using the post_save signal which calls a function cv_updated() when the user edits any CV form. Inside cv_updated(), I’m just saving the timestamp which depicts the last CV update time.

You can check how to save form data to the Django model.

This is all about the Django signals example. Similar to these there can be many use cases where you can use the Django signals.

Django Signals Events

There are multiple types of events you can use with the Django signal. Here are four signal events that are very popular and useful. I’m also adding a one-liner to explain where you can use each of them.

  • post_save

This signal event works to call a function after saving data (after save() method). It will be used even if the single field value is updated in the Django model.

  • pre_save

This signal event works to call a function just before the calling save() method.

  • post_delete

It works to call a function just after deleting the model instance (after delete() method).

  • pre_delete

It works to call a function just before deleting the model instance (before delete() method).

Any doubt? Let’s discuss this in the comment section below.

Python Interview Questions eBook

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard