So, in Django 1.7.7 a new way to handle signals was introduced. I'm using 1.7.7
with django_cms
, running on Python 2.
I'm trying to implement this new way, and even though the documentation is scarce but straightforward enough, it just won't work. I think Django-CMS or one of its plugins has something to do with it.
What I'm trying to do is increase the counter of my CounterModel
by 1 for each MyModel
that is saved on the pre_save
signal of the MyModel
model.
I've concluded it just doesn't work because a raise Exception('it runs')
in the increase_counter
function does not get raised..
I have the following:
myapp/models/mymodel.py
from .counter_model import CounterModel
# Imports here
class MyModel(models.Model):
name = models.CharField(max_length=128)
categories = models.ManyToManyField(CounterModel)
myapp/models/counter_model.py
# Imports here
class CounterModel(models.Model):
amount_of_mymodels = models.PositiveIntegerField(default=0)
myapp/signals.py
from .models.mymodel import MyModel
# Other imports here
@receiver(pre_save, sender=MyModel)
def increase_counter(sender, **kwargs):
instance = kwargs.get('instance')
for category in instance.categories.all():
category.amount_of_mymodels += 1
myapp/apps.py
from django.apps import AppConfig
# Other imports here
class MyAppConfig(AppConfig):
name = "myapp"
def ready(self):
import myapp.signals
myapp/__init__.py
default_app_config = 'myapp.apps.MyAppConfig'
The import signals
in my apps.py
is executed, because when i raise an Exception there it's raised in my console (in the ready()
function).
Hope someone can clarify this issue i'm having!
BTW: I've also added myapp
to my INSTALLED_APPS
UPDATE: I've tried the new signal approach in another project (Python 3, also Django 1.7) and it works fine. If anyone has any clue as to what could be causing the failure of signals in my other project please let me know! I'm going to try to debug this for now, every form of help is appreciated.
NOTE: For everyone thinking 'the for loop might be empty, print something', please note the following in the beginning of my question: I've concluded it just doesn't work because a raise Exception('it runs')
in the increase_counter
function does not get raised... Thanks!