Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:djangoadv [2014/06/02 16:54] alfred [Signals] |
fw:djangoadv [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 59: | Línea 59: | ||
| ===== Signals ===== | ===== Signals ===== | ||
| + | ==== Signals in Django ==== | ||
| Django provides a way of intercept special notifications... | Django provides a way of intercept special notifications... | ||
| * ''django.db.models.signals.pre_save'' & ''django.db.models.signals.post_save'', sent before or after a model’s save() method is called. | * ''django.db.models.signals.pre_save'' & ''django.db.models.signals.post_save'', sent before or after a model’s save() method is called. | ||
| Línea 64: | Línea 65: | ||
| * ''django.db.models.signals.m2m_changed'', sent when a ManyToManyField on a model is changed. | * ''django.db.models.signals.m2m_changed'', sent when a ManyToManyField on a model is changed. | ||
| * ''django.core.signals.request_started'' & ''django.core.signals.request_finished'', sent when Django starts or finishes an HTTP request. | * ''django.core.signals.request_started'' & ''django.core.signals.request_finished'', sent when Django starts or finishes an HTTP request. | ||
| + | |||
| + | There are [[https://docs.djangoproject.com/en/dev/ref/signals/|ohters]] like ''pre_init'', ''class_prepared'', ''pre_migrate'', ''pre_syncdb'', ''got_request_exception'', ''connection_created''... | ||
| + | ==== Connecting signals ==== | ||
| The way to intercept a signal is using the ''connect()'' function or the ''receiver()'' decorator: | The way to intercept a signal is using the ''connect()'' function or the ''receiver()'' decorator: | ||
| Línea 97: | Línea 101: | ||
| request_finished.connect(my_callback, dispatch_uid="my_unique_identifier") | request_finished.connect(my_callback, dispatch_uid="my_unique_identifier") | ||
| </code> | </code> | ||
| + | |||
| + | ==== Defining and sending signals ==== | ||
| + | Signals are ''django.dispatch.Signal([providing_args=list])'' instances. ''providing_args'' is a list of the names of arguments the signal will provide to listeners. To declare a ''pizza_done'' signal which provides receivers with toppics and size arguments: | ||
| + | <code python> | ||
| + | import django.dispatch | ||
| + | pizza_done = django.dispatch.Signal(providing_args=["toppings", "size"]) | ||
| + | </code> | ||
| + | |||
| + | To send a signal, call either ''Signal.send()'' or ''Signal.send_robust()''. Both return a list of tuple pairs ''[(receiver, response), ... ]''. They differ in how exceptions raised by receiver functions. ''send()'' does not catch any exceptions raised by receivers; it simply allows errors to propagate. Whereas ''send_robust()'' catches all errors derived from Python’s Exception class, and ensures all receivers are notified of the signal. | ||
| + | <code python> | ||
| + | class PizzaStore(object): | ||
| + | ... | ||
| + | def send_pizza(self, toppings, size): | ||
| + | pizza_done.send(sender=self.__class__, toppings=toppings, size=size) | ||
| + | </code> | ||
| + | To disconnect a receiver from a signal, call ''Signal.disconnect()''. | ||