4

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'
sokolata
  • 491
  • 3
  • 7
  • 21

1 Answers1

5

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
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    Hi @Willem Van Onsem, thank you for the code, but still not working. Should I install any module manually? `import apps.signals # noqa ModuleNotFoundError: No module named 'apps'` `import signals # noqa ModuleNotFoundError: No module named 'signals'` – sokolata Nov 03 '20 at 23:12
  • @sokolata: where did you save the `signals.py` file? This should be in the directory of your app. If the app has a different name, then of course you import `import my_app_name.signals`, so it should be the `signals` module of the app where you defined the signals. – Willem Van Onsem Nov 03 '20 at 23:14