Search code examples
pythondjangodjango-rest-frameworkdjango-viewsdjango-signals

Signals.py file not being recognized in django


I am working on follow/unfollow system and I need to add a signals.py file to make the follower count workl, but after I started investigating, I realized that the signals.py file was not being called because it is not on the pycache folder of the app. What can I do to make this file be recognized by django??

enter image description here

apps.py (this is what I tried)

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'
    def ready(self):
        import accounts.signals

If you need to see more code or have any questions please let me know in the comments;)


Solution

  • You need to load the module. For example in the AppConfig. In the apps.py you can specify an AppConfig [Django-doc], and load the signals with:

    # accounts/apps.py
    
    from django.apps import AppConfig
    
    
    class AccountsConfig(AppConfig):
        
        # …
    
        def ready(self):
            from accounts import signals  # noqa

    In the __init__.py you also should set this as the default AppConfig:

    # accounts/__init__.py
    
    default_app_config = 'accounts.apps.AccountsConfig'