A list of all the signals that Django sends. All built-in signals are sent
using the send()
method.
lihat juga
See the documentation on the signal dispatcher for information regarding how to register for and receive signals.
The authentication framework sends signals when a user is logged in / out.
The django.db.models.signals
module defines a set of signals sent by the
model system.
Peringatan
Many of these signals are sent by various model methods like
__init__()
or save()
that you can
override 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()
.
Catatan
Model signals sender
model can be lazily referenced when connecting a
receiver by specifying its full application label. For example, an
Answer
model defined in the polls
application could be referenced
as 'polls.Answer'
. This sort of reference can be quite handy when
dealing with circular import dependencies and swappable models.
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.
Argumen dikirim dengan dinyal ini:
sender
args
__init__()
:kwargs
__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 | Nilai |
---|---|
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.
Argumen dikirim dengan dinyal ini:
sender
instance
pre_save
¶django.db.models.signals.
pre_save
¶This is sent at the beginning of a model's save()
method.
Argumen dikirim dengan dinyal ini:
sender
instance
raw
True
if the model is saved exactly as presented
(i.e. when loading a fixture). One should not query/modify other
records in the database as the database might not be in a
consistent state yet.menggunakan
update_fields
Model.save()
, or None
if update_fields
wasn't passed to save()
.post_save
¶django.db.models.signals.
post_save
¶Like pre_save
, but sent at the end of the
save()
method.
Argumen dikirim dengan dinyal ini:
sender
instance
created
True
if a new record was created.raw
True
if the model is saved exactly as presented
(i.e. when loading a fixture). One should not query/modify other
records in the database as the database might not be in a
consistent state yet.menggunakan
update_fields
Model.save()
, or None
if update_fields
wasn't passed to save()
.pre_delete
¶django.db.models.signals.
pre_delete
¶Sent at the beginning of a model's delete()
method and a queryset's delete()
method.
Argumen dikirim dengan dinyal ini:
sender
instance
menggunakan
post_delete
¶django.db.models.signals.
post_delete
¶Like pre_delete
, but sent at the end of a model's
delete()
method and a queryset's
delete()
method.
Argumen dikirim dengan dinyal ini:
sender
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.
menggunakan
m2m_changed
¶django.db.models.signals.
m2m_changed
¶Sent when a ManyToManyField
is changed on a model
instance. Strictly speaking, this is not a model signal since it is sent by the
ManyToManyField
, but since it complements the
pre_save
/post_save
and pre_delete
/post_delete
when it comes to tracking changes to models, it is included here.
Argumen dikirim dengan dinyal ini:
sender
ManyToManyField
. This class is automatically
created when a many-to-many field is defined; you can access it using the
through
attribute on the many-to-many field.instance
sender
, or of the class the
ManyToManyField
is related to.action
A string indicating the type of update that is done on the relation. This can be one of the following:
"pre_add"
"post_add"
"pre_remove"
"post_remove"
"pre_clear"
"post_clear"
reverse
model
pk_set
For the pre_add
, post_add
, pre_remove
and post_remove
actions, this is a set 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
.
menggunakan
Sebagai contoh, jika sebuah Pizza
dapat memiliki banyak obyek Topping
, dimodelkan seperti ini:
class Topping(models.Model):
# ...
pass
class Pizza(models.Model):
# ...
toppings = models.ManyToManyField(Topping)
If we connected a handler like this:
from django.db.models.signals import m2m_changed
def toppings_changed(sender, **kwargs):
# Do something
pass
m2m_changed.connect(toppings_changed, sender=Pizza.toppings.through)
and then did something like this:
>>> p = Pizza.objects.create(...)
>>> t = Topping.objects.create(...)
>>> p.toppings.add(t)
argumen-argumen dikirim ke penangan m2m_changed
(toppings_changed
dalam contoh diatas) akan menjadi:
Argument | Nilai |
---|---|
sender |
Pizza.toppings.through (kelas m2m menengah) |
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 (kelas dari obyek ditambahkan pada Pizza ) |
pk_set |
{t.id} (sejak hanya Topping t yang ditambahkan ke hubungan) |
menggunakan |
"default" (since the default router sends writes here) |
And if we would then do something like this:
>>> t.pizza_set.remove(p)
the arguments sent to a m2m_changed
handler would be:
Argument | Nilai |
---|---|
sender |
Pizza.toppings.through (kelas m2m menengah) |
instance |
t (instance Topping sedang dirubah) |
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 (kelas dari obyek dipindahkan dari Topping ) |
pk_set |
{p.id} (since only Pizza p was removed from the
relation) |
menggunakan |
"default" (since the default router sends writes here) |
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.
Sejak sinyal ini dikirim selama pengolahan pengumpulan registrar aplikasi, dan AppConfig.ready()
berjalan setelah registrar sepenuhnya dikumpulkan, penerima tidak dapat dihubungkan di metode itu. Satu kemungkinan adalah menghubungkan mereka AppConfig.__init__()
sebagai gantinya, merawat tidak untuk mengimpor model atau panggilan pemicu pada registrar aplikasi.
Arguments that are sent with this signal:
sender
Signals sent by django-admin.
pre_migrate
¶django.db.models.signals.
pre_migrate
¶Sent by the migrate
command before it starts to install an
application. It's not emitted for applications that lack a models
module.
Argumen dikirim dengan dinyal ini:
sender
AppConfig
instance for the application about to
be migrated/synced.app_config
sender
.verbosity
Indicates how much information manage.py is printing on screen. See
the --verbosity
flag for details.
Functions which listen for pre_migrate
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
.
menggunakan
plan
The migration plan that is going to be used for the migration run. While
the plan is not public API, this allows for the rare cases when it is
necessary to know the plan. A plan is a list of two-tuples with the first
item being the instance of a migration class and the second item showing
if the migration was rolled back (True
) or applied (False
).
apps
Sebuah instance dari Apps
mengandung keadaan dari proyek sebelum perpindahan berjalan. Itu harus digunakan sebagai gantinya dari registrar apps
global untuk mengambil model yang anda ingin lakukan tindakan.
post_migrate
¶django.db.models.signals.
post_migrate
¶Sent at the end of the migrate
(even if no migrations are run) and
flush
commands. It's not emitted for applications that lack a
models
module.
Handlers of this signal must not perform database schema alterations as doing
so may cause the flush
command to fail if it runs during the
migrate
command.
Argumen dikirim dengan dinyal ini:
sender
AppConfig
instance for the application that was
just installed.app_config
sender
.verbosity
Indicates how much information manage.py is printing on screen. See
the --verbosity
flag for details.
Functions which listen for post_migrate
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
.
menggunakan
default
database.plan
The migration plan that was used for the migration run. While the plan is
not public API, this allows for the rare cases when it is necessary to
know the plan. A plan is a list of two-tuples with the first item being
the instance of a migration class and the second item showing if the
migration was rolled back (True
) or applied (False
).
apps
Sebuah instance dari Apps
mengandung keadaan dari proyek setelah perpindahan berjalan. Itu harus digunakan sebagai gantinya dari registrar apps
global untuk mengambil model yang anda ingin lakukan tindakan.
For example, you could register a callback in an
AppConfig
like this:
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def my_callback(sender, **kwargs):
# Your specific logic here
pass
class MyAppConfig(AppConfig):
...
def ready(self):
post_migrate.connect(my_callback, sender=self)
Catatan
If you provide an AppConfig
instance as the sender
argument, please ensure that the signal is registered in
ready()
. AppConfig
s are recreated for
tests that run with a modified set of INSTALLED_APPS
(such as
when settings are overridden) and such signals should be connected for each
new AppConfig
instance.
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.
Argumen dikirim dengan dinyal ini:
sender
django.core.handlers.wsgi.WsgiHandler
-- that
handled the request.environ
environ
dictionary provided to the request.request_finished
¶django.core.signals.
request_finished
¶Sent when Django finishes delivering an HTTP response to the client.
Argumen dikirim dengan dinyal ini:
sender
got_request_exception
¶django.core.signals.
got_request_exception
¶This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
Argumen dikirim dengan dinyal ini:
sender
request
HttpRequest
Signals only sent when running tests.
setting_changed
¶django.test.signals.
setting_changed
¶This signal is sent when the value of a setting is changed through the
django.test.TestCase.settings()
context manager or the
django.test.override_settings()
decorator/context manager.
It's actually sent twice: when the new value is applied ("setup") and when the
original value is restored ("teardown"). Use the enter
argument to
distinguish between the two.
You can also import this signal from django.core.signals
to avoid importing
from django.test
in non-test situations.
Argumen dikirim dengan dinyal ini:
sender
setting
value
value
is None
.enter
True
if the setting is applied, False
if restored.Signals sent by the database wrapper when a database connection is initiated.
connection_created
¶django.db.backends.signals.
connection_created
¶Sent when the database wrapper makes the initial connection to the database. This is particularly useful if you'd like to send any post connection commands to the SQL backend.
Argumen dikirim dengan dinyal ini:
sender
django.db.backends.postgresql.DatabaseWrapper
or
django.db.backends.mysql.DatabaseWrapper
, etc.connection
Des 02, 2017