Django + DjangoCMS signal handlers not being called
Question: 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!


Answer: ## Fix 1

I tested your code by putting the _signals.py_ code into _models.py_ and it
works.

**Why's that?**

From [Django
docs](https://docs.djangoproject.com/en/1.8/topics/signals/#connecting-
receiver-functions):

> **Where should this code live?**
>
> Strictly speaking, signal handling and registration code can live anywhere
> you like, although it's recommended to avoid the application's root module
> and its models module to minimize side-effects of importing code.
>
> In practice, signal handlers are usually defined in a signals submodule of
> the application they relate to. Signal receivers are connected in the
> ready() method of your application configuration class. If you're using the
> receiver() decorator, simply import the signals submodule inside ready().

## Fix 2

If you want don't want to merge put _signals.py_ code in your _models.py_ ,
you can do the following

    
    
    class MyAppConfig(...):
       ...
    
        def ready(self):
            # you've to import your app's signals.py 
            # not Django's signals module
            import myapp.signals
    

**EDIT**

There are some awesome answers and alternatives on this question: [the right
place to keep my signals.py files in
django](http://stackoverflow.com/questions/7115097/the-right-place-to-keep-my-
signals-py-files-in-django)

