SinyalLink to this heading
A list of all the signals that Django sends. All built-in signals are sent
using the send() method.
Model signalsLink to this heading
The django.db.models.signals module defines a set of signals sent by the
model system.
pre_initLink to this heading
- django.db.models.signals.pre_initLink to this definition
Whenever you instantiate a Django model, this signal is sent at the beginning
of the model's __init__() method.
Argumen dikirim dengan dinyal ini:
senderThe model class that just had an instance created.
argsA list of positional arguments passed to
__init__():kwargsA 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 |
Nilai |
|---|---|
|
|
|
|
|
|
post_initLink to this heading
- django.db.models.signals.post_initLink to this definition
Like pre_init, but this one is sent when the __init__() method finishes.
Argumen dikirim dengan dinyal ini:
senderAs above: the model class that just had an instance created.
instanceThe actual instance of the model that's just been created.
pre_saveLink to this heading
- django.db.models.signals.pre_saveLink to this definition
This is sent at the beginning of a model's save()
method.
Argumen dikirim dengan dinyal ini:
senderThe model class.
instanceThe actual instance being saved.
rawA boolean;
Trueif 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.menggunakanThe database alias being used.
update_fieldsThe set of fields to update as passed to
Model.save(), orNoneifupdate_fieldswasn't passed tosave().
post_saveLink to this heading
- django.db.models.signals.post_saveLink to this definition
Like pre_save, but sent at the end of the
save() method.
Argumen dikirim dengan dinyal ini:
senderThe model class.
instanceThe actual instance being saved.
createdA boolean;
Trueif a new record was created.rawA boolean;
Trueif 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.menggunakanThe database alias being used.
update_fieldsThe set of fields to update as passed to
Model.save(), orNoneifupdate_fieldswasn't passed tosave().
pre_deleteLink to this heading
- django.db.models.signals.pre_deleteLink to this definition
Sent at the beginning of a model's delete()
method and a queryset's delete() method.
Argumen dikirim dengan dinyal ini:
senderThe model class.
instanceThe actual instance being deleted.
menggunakanThe database alias being used.
post_deleteLink to this heading
- django.db.models.signals.post_deleteLink to this definition
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:
senderThe model class.
instanceThe 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.
menggunakanThe database alias being used.
m2m_changedLink to this heading
- django.db.models.signals.m2m_changedLink to this definition
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:
senderThe intermediate model class describing the
ManyToManyField. This class is automatically created when a many-to-many field is defined; you can access it using thethroughattribute on the many-to-many field.instanceThe instance whose many-to-many relation is updated. This can be an instance of the
sender, or of the class theManyToManyFieldis related to.actionA string indicating the type of update that is done on the relation. This can be one of the following:
"pre_add"Sent before one or more objects are added to the relation.
"post_add"Sent after one or more objects are added to the relation.
"pre_remove"Sent before one or more objects are removed from the relation.
"post_remove"Sent after one or more objects are removed from the relation.
"pre_clear"Sent before the relation is cleared.
"post_clear"Sent after the relation is cleared.
reverseIndicates which side of the relation is updated (i.e., if it is the forward or reverse relation that is being modified).
modelThe class of the objects that are added to, removed from or cleared from the relation.
pk_setFor the
pre_add,post_add,pre_removeandpost_removeactions, this is a set of primary key values that have been added to or removed from the relation.For the
pre_clearandpost_clearactions, this isNone.menggunakanThe database alias being used.
For example, if a Pizza can have multiple Topping objects, modeled
like this:
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)
the arguments sent to a m2m_changed handler (toppings_changed in
the example above) would be:
Argument |
Nilai |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class_preparedLink to this heading
- django.db.models.signals.class_preparedLink to this definition
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.
Since this signal is sent during the app registry population process, and
AppConfig.ready() runs after the app
registry is fully populated, receivers cannot be connected in that method.
One possibility is to connect them AppConfig.__init__() instead, taking
care not to import models or trigger calls to the app registry.
Arguments that are sent with this signal:
senderKelas model yang telah dipersiapkan.
Pengelolaan sinyalLink to this heading
Signals sent by django-admin.
pre_migrateLink to this heading
- django.db.models.signals.pre_migrateLink to this definition
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:
senderAn
AppConfiginstance for the application about to be migrated/synced.app_configSame as
sender.verbosityIndicates how much information manage.py is printing on screen. See the
--verbosityflag for details.Functions which listen for
pre_migrateshould adjust what they output to the screen based on the value of this argument.interactiveIf
interactiveisTrue, it's safe to prompt the user to input things on the command line. IfinteractiveisFalse, functions which listen for this signal should not try to prompt for anything.For example, the
django.contrib.authapp only prompts to create a superuser wheninteractiveisTrue.menggunakanThe alias of database on which a command will operate.
post_migrateLink to this heading
- django.db.models.signals.post_migrateLink to this definition
Sent by the migrate command after it installs an application, and the
flush command. It's not emitted for applications that lack a
models module.
It is important that handlers of this signal perform idempotent changes (e.g.
no database alterations) as this may cause the flush management
command to fail if it also ran during the migrate command.
Argumen dikirim dengan dinyal ini:
senderAn
AppConfiginstance for the application that was just installed.app_configSame as
sender.verbosityIndicates how much information manage.py is printing on screen. See the
--verbosityflag for details.Functions which listen for
post_migrateshould adjust what they output to the screen based on the value of this argument.interactiveIf
interactiveisTrue, it's safe to prompt the user to input things on the command line. IfinteractiveisFalse, functions which listen for this signal should not try to prompt for anything.For example, the
django.contrib.authapp only prompts to create a superuser wheninteractiveisTrue.menggunakanThe database alias used for synchronization. Defaults to the
defaultdatabase.
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)
Sinyal permintaan/tanggapanLink to this heading
Signals sent by the core framework when processing a request.
request_startedLink to this heading
- django.core.signals.request_startedLink to this definition
Sent when Django begins processing an HTTP request.
Argumen dikirim dengan dinyal ini:
senderThe handler class -- e.g.
django.core.handlers.wsgi.WsgiHandler-- that handled the request.environThe
environdictionary provided to the request.
request_finishedLink to this heading
- django.core.signals.request_finishedLink to this definition
Sent when Django finishes delivering an HTTP response to the client.
Argumen dikirim dengan dinyal ini:
senderThe handler class, as above.
got_request_exceptionLink to this heading
- django.core.signals.got_request_exceptionLink to this definition
This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
Argumen dikirim dengan dinyal ini:
senderThe handler class, as above.
requestObyek
HttpRequest
Test signalsLink to this heading
Signals only sent when running tests.
setting_changedLink to this heading
- django.test.signals.setting_changedLink to this definition
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:
senderThe settings handler.
settingThe name of the setting.
valueThe value of the setting after the change. For settings that initially don't exist, in the "teardown" phase,
valueisNone.enterA boolean;
Trueif the setting is applied,Falseif restored.
template_renderedLink to this heading
- django.test.signals.template_renderedLink to this definition
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.
Argumen dikirim dengan dinyal ini:
Database WrappersLink to this heading
Signals sent by the database wrapper when a database connection is initiated.
connection_createdLink to this heading
- django.db.backends.signals.connection_createdLink to this definition
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:
senderThe database wrapper class -- i.e.
django.db.backends.postgresql.DatabaseWrapperordjango.db.backends.mysql.DatabaseWrapper, etc.connectionThe database connection that was opened. This can be used in a multiple-database configuration to differentiate connection signals from different databases.