liveBook · Manning

Observer pattern

As explained in Django in Action

The observer/observable pattern is a design pattern in which observers register interest in an observable and receive notifications when its state or activity changes.

The observer/observable pattern lets one component register interest in an action and receive notification when that action occurs. Django signals apply this model to ORM events, so related work can happen automatically without repeating checks throughout application code. section 6.5

Django model signals provide a practical observer workflow:

  • Choose an event such as post_save and register a handler with the receiver decorator. section 6.5
  • Connect the signal to a sender, such as the User model, whose actions matter. section 6.5

Creating a user profile automatically

A post_save handler checks created and raw, retrieves the new User instance, verifies that no UserProfile already exists, and creates one when needed. This covers users created in application code, the Django Admin, or fixture loading. section 6.5

How Django in Action covers it

How deep it goes
Explains registration, senders, pre/post timing, creation checks, and duplicate protection, but does not develop a general signal architecture.
Examples
Demonstrates a post_save User handler that creates a missing UserProfile across application, Admin, and fixture workflows.
What it assumes
Assumes familiarity with Django models and the ORM while distinguishing first saves, updates, Admin creation, and fixture loading.
What you can do after
Build it from scratch by registering a Django signal handler and safely creating a related model as a save side effect.
How it connects
Connects observer pattern with Django signals, the Django ORM, model events, and UserProfile creation.

Also explained in

Compare how 4 books explain observer pattern →

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.