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?