Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django `sender` not working for signal receiver

I am trying to create a post_save handler for when the model Client has been saved. The reason I am using a signal is because I require for the code run when a Client is added via loaddata as well.

Heres the sender:

# apps.py
from django.db.models.signals import post_save


class ClientsConfig(AppConfig):
    name = 'clients'
    verbose_name = "clients"

    def ready(self):
        """https://docs.djangoproject.com/en/4.1/topics/signals/"""
        # Implicitly connect signal handlers decorated with @receiver.
        from . import signals
        # Explicitly connect a signal handler.
        post_save.connect(signals.write_svg, dispatch_uid="clients_client_post_save")

Heres the receiver:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Client


@receiver(post_save, sender=Client)
def write_svg(sender, instance, **kwargs):
    """Using a signal so that when one loaddata the DB, it performs this action too"""
    print('\n-----------------------------------------------------')
    print('Signal called, sender', sender)

Here you can see it prints for a whole bunch of Session events, and many other events, instead of being filtered for only the client model:

-----------------------------------------------------
Signal called, sender <class 'django.contrib.sessions.models.Session'>

-----------------------------------------------------
Signal called, sender <class 'django.contrib.sessions.models.Session'>

-----------------------------------------------------
Signal called, sender <class 'django.contrib.sessions.models.Session'>

-----------------------------------------------------
Signal called, sender <class 'django.contrib.sessions.models.Session'>

-----------------------------------------------------
Signal called, sender <class 'django.contrib.sessions.models.Session'>

>Solution :

You have to remove the

post_save.connect(signals.write_svg, dispatch_uid="clients_client_post_save")

You have already used the receivers decorator, you do not need to use the connect function too.

The first comment on your ready method explains this behaviour 😉

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading