0Pricing
Django Academy · Lesson

post_save and pre_delete Signals

Run code when models change.

post_save and pre_delete Signals is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Signals in a Sentence

A signal lets one part of Django announce that something happened, so other code can react without being directly called. 

Why React to Events

Sometimes you want side effects when a model changes, like sending a welcome email after signup. Signals keep that reaction out of your save logic.

The post_save Signal

The post_save signal fires right after a model instance is written to the database, so you know the row already exists.

A post_save Receiver

You write a function and tell Django to call it when post_save fires. The receiver gets the saved instance through its arguments.

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

@receiver(post_save, sender=Profile)
def on_save(sender, instance, **kwargs):
    print("saved", instance)

The created Flag

post_save passes a created boolean: True on a brand-new row, False on an update. Use it to run code only on first creation.

def on_save(sender, instance, created, **kwargs):
    if created:
        print("new", instance)

The pre_delete Signal

The pre_delete signal fires just before a row is removed, so the instance still exists when your code runs.

A pre_delete Receiver

Use pre_delete to clean up related resources, like a file, before the database row vanishes. The instance is still fully available.

from django.db.models.signals import pre_delete

@receiver(pre_delete, sender=Photo)
def clean_up(sender, instance, **kwargs):
    instance.image.delete(save=False)

pre_save and post_delete Too

Django also ships pre_save and post_delete. Together they cover the moments just before and after each write or delete.

The sender Argument

The sender tells you which model class triggered the signal. Pass sender= to a receiver so it only listens to one model.

Always Accept kwargs

Every receiver should accept **kwargs. Django may pass extra arguments, and missing them raises an error when the signal fires.

Decouple, Don't Bury Logic

Signals shine for cross-cutting reactions, but heavy logic can become hard to trace. Reach for a signal when the reaction is truly separate.

Quick Check

Test how post_save tells new rows apart from updates.

Recap: Reacting to Model Events

You met Django signals: post_save fires after writes with a created flag, pre_delete fires before removals, and every receiver should accept **kwargs. ✅

Frequently asked questions

Is the “post_save and pre_delete Signals” lesson free?

Yes — the full text of “post_save and pre_delete Signals” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “post_save and pre_delete Signals”?

Run code when models change. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “post_save and pre_delete Signals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. post_save and pre_delete Signals
  2. Connecting Receivers Properly
  3. Writing Custom Middleware
  4. Middleware Order and process_view
← Back to Django Academy