Django v1.1 documentation

Built-in signal reference

A list of all the signals that Django sends.

Model signals

The django.db.models.signals module defines a set of signals sent by the module system.

Warning

Many of these signals are sent by various model methods like __init__() or save() that you can overwrite in your own code.

If you override these methods on your model, you must call the parent class’ methods for this signals to be sent.

Note also that Django stores signal handlers as weak references by default, so if your handler is a local function, it may be garbage collected. To prevent this, pass weak=False when you call the signal’s connect().

pre_init

django.db.models.signals.pre_init

Whenever you instantiate a Django model,, this signal is sent at the beginning of the model’s __init__() method.

Arguments sent with this signal:

sender
The model class that just had an instance created.
args
A list of positional arguments passed to __init__():
kwargs
A dictionary of keyword arguments passed to __init__():.

For example, the tutorial has this line:

p = Poll(question="What's up?", pub_date=datetime.now())

The arguments sent to a pre_init handler would be:

Argument Value
sender Poll (the class itself)
args [] (an empty list because there were no positional arguments passed to __init__.)
kwargs {'question': "What's up?", 'pub_date': datetime.now()}

post_init

django.db.models.signals.post_init

Like pre_init, but this one is sent when the __init__(): method finishes.

Arguments sent with this signal:

sender
As above: the model class that just had an instance created.
instance
The actual instance of the model that's just been created.

pre_save

django.db.models.signals.pre_save

This is sent at the beginning of a model's save() method.

Arguments sent with this signal:

sender
The model class.
instance
The actual instance being saved.

post_save

django.db.models.signals.post_save

Like pre_save, but sent at the end of the save() method.

Arguments sent with this signal:

sender
The model class.
instance
The actual instance being saved.
created
A boolean; True if a new record was create.

pre_delete

django.db.models.signals.pre_delete

Sent at the beginning of a model's delete() method.

Arguments sent with this signal:

sender
The model class.
instance
The actual instance being deleted.

post_delete

django.db.models.signals.post_delete

Like pre_delete, but sent at the end of the delete() method.

Arguments sent with this signal:

sender
The model class.
instance

The actual instance being deleted.

Note that the object will no longer be in the database, so be very careful what you do with this instance.

class_prepared

django.db.models.signals.class_prepared

Sent whenever a model class has been "prepared" -- that is, once model has been defined and registered with Django's model system. Django uses this signal internally; it's not generally used in third-party applications.

Arguments that are sent with this signal:

sender
The model class which was just prepared.

Management signals

Signals sent by django-admin.

post_syncdb

django.db.models.signals.post_syncdb

Sent by syncdb after it installs an application.

Any handlers that listen to this signal need to be written in a particular place: a management module in one of your INSTALLED_APPS. If handlers are registered anywhere else they may not be loaded by syncdb.

Arguments sent with this signal:

sender
The models module that was just installed. That is, if syncdb just installed an app called "foo.bar.myapp", sender will be the foo.bar.myapp.models module.
app
Same as sender.
created_models
A list of the model classes from any app which syncdb has created so far.
verbosity

Indicates how much information manage.py is printing on screen. See the --verbosity flag for details.

Functions which listen for post_syncdb should adjust what they output to the screen based on the value of this argument.

interactive

If interactive is True, it's safe to prompt the user to input things on the command line. If interactive is False, functions which listen for this signal should not try to prompt for anything.

For example, the django.contrib.auth app only prompts to create a superuser when interactive is True.

Request/response signals

Signals sent by the core framework when processing a request.

request_started

django.core.signals.request_started

Sent when Django begins processing an HTTP request.

Arguments sent with this signal:

sender
The handler class -- i.e. django.core.handlers.modpython.ModPythonHandler or django.core.handlers.wsgi.WsgiHandler -- that handled the request.

request_finished

django.core.signals.request_finished

Sent when Django finishes processing an HTTP request.

Arguments sent with this signal:

sender
The handler class, as above.

got_request_exception

django.core.signals.got_request_exception

This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.

Arguments sent with this signal:

sender
The handler class, as above.
request
The HttpRequest object.

Test signals

Signals only sent when running tests.

template_rendered

django.test.signals.template_rendered

Sent when the test system renders a template. This signal is not emitted during normal operation of a Django server -- it is only available during testing.

Arguments sent with this signal:

sender
The Template object which was rendered.
template
Same as sender
context
The Context with which the template was rendered.