1

I've have got a usecase in which I have a model named "Content" which inherits from a 3rd party library I've installed as a pip package known as "django-mptt" as shown below.

content_viewer/models.py

class Content(MPTTModel):
    content_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    content_name = models.CharField(max_length=250)
    parent = TreeForeignKey(
        "self",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name="children",
    )

I'm also using redis cache for cacheops and I've added the configurations as follow in my settings.py.

dashboard/settings.py

CACHEOPS_REDIS = {
    "host": REDIS_HOST,
    "port": REDIS_PORT,
    "db": 1,
    "socket_timeout": 3,
}

CACHEOPS = {
    "content_viewer.*": {"ops": {"fetch", "get"}, "timeout": 60 * 60},
}

Note:- content_viewer is the name of the app

Requirement: What I want here is a post_save signal on the MPTTModel class which should invalidate/clear the redis cache for the object that has been created/saved

I've created a signal in my signals.py file as below.

content_viewer/signals.py

@receiver(post_save, sender=MPTTModel)
def clear_redis_cache_for_mptt_instance(sender, instance, **kwargs):
    invalidate_obj(instance)

and I've imported the signals inside the apps.py file as shown below.

content_viewer/apps.py

class WorkspaceDisplayConfig(AppConfig):
    name = "content_viewer"

    def ready(self):
        import content_viewer.signals

But when I run the code in debug mode the signal seems to be never fired whenever I create an instance of the Content object.

can anyone tell me what could I be doing wrong here?

nick
  • 439
  • 1
  • 4
  • 17
  • Have you debugged your app to make sure the `ready` method is being run and that `signals.py` is run too? – Iain Shelvington Nov 19 '21 at 07:57
  • Yes @lain I've debugged the app and the ready method and the signals.py is being run – nick Nov 19 '21 at 08:01
  • And how are you creating/saving the instance? Can you share the code? – Iain Shelvington Nov 19 '21 at 08:01
  • I'm adding the instance from the admin panel – nick Nov 19 '21 at 08:02
  • Just as a test, if you remove `sender=Content` from `@receiver` and you just log/print sender and instance does your function receive signals for other models? What I'm getting at is, does the handler not get registered correctly or does the model genuinely not send the signal – Iain Shelvington Nov 19 '21 at 08:15
  • Yes, my function is receiving signals for other models. – nick Nov 19 '21 at 08:36
  • Can you share your admin that is not triggering the signal? – Iain Shelvington Nov 19 '21 at 09:28
  • @IainShelvington I've updated the real question there was a mistake the signal was supposed to receive the trigger from the MPTTModel class – nick Nov 19 '21 at 10:27
  • @IainShelvington what do you mean by sharing the admin which is not triggering the signal? – nick Nov 19 '21 at 10:28
  • 1
    `MPTTModel` is abstract, you can't create instances of it so you'll never get a signal from it. Do you want to receive signals from all subclasses of `MPTTModel` instead? – Iain Shelvington Nov 19 '21 at 10:34
  • Oh I see, so that is the reason I'm not getting any signals for the MPTTModel – nick Nov 19 '21 at 10:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239391/discussion-between-nick-and-iain-shelvington). – nick Nov 19 '21 at 10:38

0 Answers0