Home > Enterprise >  Django signals how to notify user on new reply?
Django signals how to notify user on new reply?

Time:01-26

Right now my author get notification through signals if anyone replied or comment. Let explain you this line of code little bit

if instance.user.id != instance.blog.author.id: only triggering signals if user is not blog author. here is my code:

models.py

class BlogComment(models.Model):
      blog = models.ForeignKey(Blog,on_delete=models.CASCADE,blank=True,null=True)
      user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,blank=True,null=True) 



@receiver(post_save,sender=BlogComment)
def NotiFicationSignals(instance,created,**kwargs):
    if created:
             if instance.user.id != instance.blog.author.id: #notifiy author 
                 noti_to_author = Notifications.objects.create(duplicate_value="author",sender=instance.user,receiver=instance.blog.author,text_preview=f"Recived new comment from {instance.user}")
          

Now I want to create notifications object for commenter if any reply added on his comment.

CodePudding user response:

add an optional reply_to foreign_key field to your blog comment like:

reply_to= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="replies_to_me", blank=True,null=True) 

then do this in your signals

noti_to_commenter = Notifications.objects.create(duplicate_value="author",sender=instance.user,receiver=instance.reply_to,text_preview=f"Recived new comment from {instance.user}")

CodePudding user response:

Somehow, you need to keep track of parent comment (initial comment that can be replied). I can suggest one way based on your given model schema:

You can add parent_comment field in BlogComment model. This field can be empty for parent comment but you need to set this field for child or replied comments. This field will point to parent BlogComment model and you can extract user information from this field.

In this scenario your code will look like this:

class BlogComment(models.Model):
  blog = models.ForeignKey(Blog,on_delete=models.CASCADE,blank=True,null=True)
  user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,blank=True,null=True) 
  parent_comment = models.ForeignKey('self', on_delete=models.CASCADE,blank=True,null=True) 

@receiver(post_save,sender=BlogComment)
def NotiFicationSignals(instance,created,**kwargs):
    if created:
             if instance.user.id != instance.blog.author.id: #notify author 
                 noti_to_author = Notifications.objects.create(duplicate_value="author",sender=instance.user,receiver=instance.blog.author,text_preview=f"Recived new comment from {instance.user}")
             if instance.parent_comment: # notify to parent commenter 
                 noti_to_parent_commenter = Notifications.objects.create(duplicate_value="parent commenter",sender=instance.user,receiver=instance.parent_comment.user,text_preview=f"Recived new comment from {instance.user}")
      

Similar database structure is proposed in this thread Posts, comments, replies, and likes database schema

  •  Tags:  
  • Related