Search code examples
pythondjangodjango-signals

Why is my SIGNAL not working in Django - what I'm doing wrong?


first of all, I'm not a developer.

Trying to build an Application with Django (Version 3.1.2) but facing some issues with signals.

I have this Models in my models.py:

class PhoneNumbers(models.Model):
    number = models.CharField(_('Category'), max_length=255)
    created = models.DateTimeField(_('Created'), auto_now_add=True, blank=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

and a Model Persons

class Persons(models.Model):
    name = models.CharField(_('Name'), max_length=255)
    number = models.CharField(_(Number), max_length=255)
    ...

The code in my signals.py:

from django.db.models.signals import pre_save, post_delete, post_save
from django.dispatch import receiver
from .models import PhoneNumbers, Persons

@receiver(post_save, sender=Persons)
def save_contract(instance, sender, created, **kwargs):
    print("Request finished!")

When I save a Person I expect to get a PRINT in the console output, but get nothing. What is wrong?

I also add in __init__.py:

default_app_config = 'myapp.apps.MyAppConfig'

My apps.py looks like:

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    name = 'myapp'

Solution

  • You did not load the signals module. You can do this in the MyAppConfig:

    from django.apps import AppConfig
    
    
    class MyAppConfig(AppConfig):
        name = 'myapp'
    
        def ready(self):
            import apps.signals  # noqa