liveBook · Manning

Post_save

As explained in Django in Action

post_save is a Django ORM signal emitted after a model instance has been saved.

post_save is a Django ORM signal emitted after a model instance has been saved. It follows the observer pattern: register interest in a model action, then let a handler perform a side effect when that action occurs. section 6.5

The signal reports saves, not creates. A handler must inspect created to distinguish first-time creation from updates, and raw to avoid acting while a fixture is being loaded. The instance argument identifies the saved object. section 6.5

Create a profile after creating a user

from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=User) def user_post_save(sender, **kwargs): if kwargs['created'] and not kwargs['raw']: user = kwargs['instance'] try: UserProfile.objects.get(user=user) except UserProfile.DoesNotExist: UserProfile.objects.create(user=user) The receiver runs after a User is saved, skips updates and fixture loads, checks that a profile does not already exist, and creates one only when necessary. section 6.5

A practical handler accounts for several creation paths:

  • Application code can create the model. section 6.5
  • Django Admin can create the model. section 6.5
  • Fixture loading can create the model, so raw prevents the side effect. section 6.5

How Django in Action covers it

How deep it goes
Explains observer registration, receiver arguments, creation checks, fixture handling, and duplicate prevention, but does not develop broader signal usage.
Examples
Uses a User receiver that conditionally creates a UserProfile and verifies the resulting relationship in the Django shell.
What it assumes
Assumes basic Django models and ORM objects while teaching signal registration, receiver arguments, and creation-versus-update handling.
What you can do after
Build it from scratch by registering a receiver that creates a related object after a qualifying model save.
How it connects
Connects Django ORM signals with the observer pattern, pre_save, User, UserProfile, Django Admin, and fixtures.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.