См.также
See the documentation on the signal dispatcher for information regarding how to register for and receive signals.
The comment framework sends a set of comment-related signals.
The authentication framework sends signals when a user is logged in / out.
Предупреждение
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().
p = Poll(question="What's up?", pub_date=datetime.now())
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()} |
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.
A string indicating the type of update that is done on the relation. This can be one of the following:
For the pre_add, post_add, pre_remove and post_remove actions, this is a list of primary key values that have been added to or removed from the relation.
For the pre_clear and post_clear actions, this is None.
class Topping(models.Model):
# ...
pass
class Pizza(models.Model):
# ...
toppings = models.ManyToManyField(Topping)
>>> p = Pizza.object.create(...)
>>> t = Topping.objects.create(...)
>>> p.toppings.add(t)
Argument | Value |
---|---|
sender | Pizza.toppings.through (the intermediate m2m class) |
instance | p (the Pizza instance being modified) |
action | "pre_add" (followed by a separate signal with "post_add") |
reverse | False (Pizza contains the ManyToManyField, so this call modifies the forward relation) |
model | Topping (the class of the objects added to the Pizza) |
pk_set | [t.id] (since only Topping t was added to the relation) |
using | "default" (since the default router sends writes here) |
>>> t.pizza_set.remove(p)
Argument | Value |
---|---|
sender | Pizza.toppings.through (the intermediate m2m class) |
instance | t (the Topping instance being modified) |
action | "pre_remove" (followed by a separate signal with "post_remove") |
reverse | True (Pizza contains the ManyToManyField, so this call modifies the reverse relation) |
model | Pizza (the class of the objects removed from the Topping) |
pk_set | [p.id] (since only Pizza p was removed from the relation) |
using | "default" (since the default router sends writes here) |
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.
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.
from django.db.models.signals import post_syncdb
import yourapp.models
def my_callback(sender, **kwargs):
# Your specific logic here
pass
post_syncdb.connect(my_callback, sender=yourapp.models)
Aug 21, 2013