I am working on django project and I have written a post_save signal to notify post author when commented. Now, I want to add a post_save signal to notify post author & comment user that a comment (reply) is made. How do I approach this? This is because there is a parent-child relationship on my comment model.
Below are my model and signal code. Can someone assist please? I am new on django and have just started learning django signals.
class Comment(models.Model):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
post = models.ForeignKey('forum.Post',related_name='comments', on_delete=models.CASCADE)
reply = models.ForeignKey('self', null=True, blank=True, related_name='replies', on_delete=models.CASCADE)
text = models.TextField(max_length=250, blank=True)
@receiver(post_save, sender=Comment)
def user_comment_post(sender, instance, created, **kwargs):
if created:
comment = instance
post = comment.post
#reply = comment.reply
text_preview = comment.text[:90]
sender = comment.user
notify = Notification.objects.create(post=post, sender=sender, comment=comment, user=post.author, text_preview=text_preview, notification_type=2)
notify.save()