2

I am trying to implement signals for my app and my directory structure looks like this

- src
    - utility_apps
        __init__.py
        - posts
            - migrations
            - static
            - templates
            admin.py
            views.py
            apps.py
            signals.py
            models.py
            __init__.py
            ......

    static
    manage.py
    ......

Here posts is my app and the signals.py is inside its folder, But my signals aren't working. I have defined my signal code as -

from .models import Post
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Post)
def give_group_owner_permission(sender, instance, created, **kwargs):
    print("coming to signal")
    if created:
        print("created")

But it doesn't work. In my apps.py I have changed the ready function as

class Post(AppConfig):
    name = 'post'

    def ready(self):
        import utility_apps.posts.signals

I have even tried importing posts.signal in the ready function. What I am doing wrong here, please help

My installed apps look like below

INSTALLED_APPS = [
    'utility_apps.posts',
    'mainapp',
     .....
]
Shahrukh Mohammad
  • 985
  • 2
  • 17
  • 33

1 Answers1

3

The following solution worked for me.

The first change which I had to make was put a default_app_config value in my __init__.py file of my posts app as

default_app_config = 'utility_apps.posts.apps.PostsConfig'

And then I had to change PostsConfig class as

class PostsConfig(AppConfig):
    name = 'utility_apps.posts'

    def ready(self):
        import utility_apps.posts.signals

Basically I had to change two things -

  1. The name which was set to posts by default
  2. Change the ready function and import my signals in it

It worked for me. Alternatively, I could have also included my PostsConfig in my installed app.

Shahrukh Mohammad
  • 985
  • 2
  • 17
  • 33