how to send a django signal from other signal

  • Last Update :
  • Techknowledgy :

Although this may be possible by calling a signal manually from inside another signal like this:

post_save.send(MyModel, instance = a_mymodel_instance)

Organize your signals as follows:

@receiver(post_save, sender = MyModel1)
def mymodel1_signal(sender, instance, ** kwargs):
   Do stuff on MyModel1 instance...
   Do also stuff on MyModel2 and then call MyModel2.save()

@receiver(post_save, sender = MyModel2)
def mymodel2_signal(sender, instance, ** kwargs):
   Do stuff on MyModel2 instance...

Use a pre_save and a post_save signal:

@receiver(pre_save, sender = MyModel1)
def mymodel1_signal(sender, instance, ** kwargs):
   Do stuff on MyModel1 instance...
   Do also stuff on MyModel2 and then call MyModel2.save()

@receiver(post_save, sender = MyModel2)
def mymodel2_signal(sender, instance, ** kwargs):
   Do stuff on MyModel2 instance...

Organize your signals as follows:

@receiver(post_save, sender = MyModel1)
def mymodel1_signal(sender, instance, ** kwargs):
   Do stuff on MyModel1 instance...
   Do also stuff on MyModel2 and then call MyModel2.save()

@receiver(post_save, sender = MyModel2)
def mymodel2_signal(sender, instance, ** kwargs):
   Do stuff on MyModel2 instance...

Use a pre_save and a post_save signal:

@receiver(pre_save, sender = MyModel1)
def mymodel1_signal(sender, instance, ** kwargs):
   Do stuff on MyModel1 instance...
   Do also stuff on MyModel2 and then call MyModel2.save()

@receiver(post_save, sender = MyModel2)
def mymodel2_signal(sender, instance, ** kwargs):
   Do stuff on MyModel2 instance...

Suggestion : 2

To receive a signal, register a receiver function using the Signal.connect() method. The receiver function is called when the signal is sent. All of the signal’s receiver functions are called one at a time, in the order they were registered.,receiver – The callback function which will be connected to this signal. See Receiver functions for more information.,First, we need to define a receiver function. A receiver can be any Python function or method:,Listening to signals Receiver functions Connecting receiver functions Connecting to signals sent by specific senders Preventing duplicate signals

def my_callback(sender, ** kwargs):
   print("Request finished!")
from django.core.signals
import request_finished

request_finished.connect(my_callback)
from django.core.signals
import request_finished
from django.dispatch
import receiver

@receiver(request_finished)
def my_callback(sender, ** kwargs):
   print("Request finished!")
from django.apps
import AppConfig
from django.core.signals
import request_finished

class MyAppConfig(AppConfig):
   ...

   def ready(self):
   # Implicitly connect signal handlers decorated with @receiver.
from.import signals
# Explicitly connect a signal handler.
request_finished.connect(signals.my_callback)
from django.db.models.signals
import pre_save
from django.dispatch
import receiver
from myapp.models
import MyModel

@receiver(pre_save, sender = MyModel)
def my_handler(sender, ** kwargs):
   ...
from django.core.signals
import request_finished

request_finished.connect(my_callback, dispatch_uid = "my_unique_identifier")

Suggestion : 3

Last Updated : 07 Oct, 2021,GATE CS 2021 Syllabus

If you just use post_save.connect(my_function), then it will get fired as soon as any save method is fired.

post_save.connect(my_function_post_save, sender = MyModel)
pre_save.connect(my_function, sender = UserTextMessage)

Suggestion : 4

DSN or django-signal-notifier is a Django package to send message or notification based on the Django's signals triggering. You can assign some backends to each signal(e.g., An In-Site notification app).,The major difference between django-signal-notifier and other Django's notification packages is that DSN isn't just a simple message delivering system. It can act as a middleware between Django and every messenger client (Various clients like email, telegram, SMS and twitter).,It's working with event methodology, and it's based on Django signal. If a signal triggers, A messenger is called to send a message for specified users. To understand how it works, We explain some main concepts at first.,You should pay attention to these 3 questions when you want to assign a new trigger to a signal.

1. Install django-signal-notifier by pip:

$ pip install django - signal - notifier

or use the source

$ git clone https: //github.com/hadi2f244/django-signal-notifier
   $ cd django - signal - notifier
$ python setup.py sdist
$ pip install dist / django - signal - notifier *
  1. Add "django_signal_notifier" at the end of INSTALLED_APPS setting like this
INSTALLED_APPS = [
   'django.contrib.auth',
   'django.contrib.contenttypes',
   ...
   'django_signal_notifier',
]
5._
from django_signal_notifier.models
import *
TestModel1_another_instance = TestModel1.objects.create(name = "new_test_model2", extra_field = "extra")
no such table: django_signal_notifier_trigger.
An error occurs when reconnecting trigger to the corresponding signals, Note: Make sure you migrate and migrations first
from django_signal_notifier.models
import *
TestModel1_another_instance = TestModel1.objects.create(name = "new_test_model2", extra_field = "extra")