Django signals not working properly

I've used signals in some of my projects and I allways import the signals in the __init__.py of my Django APP (Same folder as settings.py, views.py, urls.py...)

__init__.py:

import signals

signals.py:

from django.db.models.signals import post_save, pre_delete 
from django.dispatch import receiver
from my_project.models import *


@receiver(post_save, sender=Modelname)  # Called after an object is saved
def create_modelname(sender, **kwargs):
    obj = kwargs['instance']  # I get the object being saved here
    # ... Here I do whatever I want

@receiver(pre_delete, sender=Modelname)  # Called before an object is deleted
def delete_modelname(sender, **kwargs):
   obj = kwargs['instance']
   # ... Do whatever you need

Remember this 2 imports:

  • from django.db.models.signals import post_save, pre_delete
  • from django.dispatch import receiver

Remember to import the signals

  • To import the signals you need to add import signals in your __init__.py of your project

Using this code, this functions are called automatically by Django when an object of the class Modelname is created or deleted.

The receiver for created object is called after the object is created, and the receiver for deleted object is called before the object is deleted.


I think maybe you just need to import your helpers/status.py eg in models/__init__.py

otherwise your event_status signal gets defined ok but the signal handler create_status never gets connected by Django

if you only have one handler for that signal it might make sense to put it in the same module as the signal definition

Tags:

Django

Signals