Django 3.0.5.
apps.py
from django.apps import AppConfig
from django.db.models.signals import post_save
from django.dispatch import receiver
class NewsConfig(AppConfig):
name = 'news'
def ready(self):
from .models import News # Breakpoint 0
@receiver(post_save, sender=News)
def handle_news_save(sender, **kwargs):
print("Working")
a = 0 # Breakpoint 1
models.py
class News(models.Model):
news_text = models.TextField()
settings.py
INSTALLED_APPS = [
...
'news.apps.NewsConfig',
]
The problem
At Breakpoint 0 the interpreter stops when I run the application. That is at the next line Django gets to know that I'm catching the signal.
But when I save an instance of News in admin site, at Breakpoint 1 the interpreter doesn't stop. And, of course, no printing happens. Could you help me catch the signal?
Ok i tried it out and played with signals a little, here's what i found out:
As the question state this way of defining does not work it seems to not register the signal correctly. I have no clue why it doesn't.
I guess the convention about signals would be anyways to move them to a signals.py
file and then in the apps.py
you only import them.
signals.py:
from .models import News # Breakpoint 0
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=News)
def handle_news_save(sender, **kwargs):
print("Working")
a = 0 # Breakpoint 1
apps.py:
from django.apps import AppConfig
from django.db.models.signals import post_save
from django.dispatch import receiver
class PollsConfig(AppConfig):
name = 'polls'
def ready(self):
import polls.signals
and then it works