[英]How to get the total number of Likes related to an Author and use it to send notifications when reaching a certain number
I have a notification system in my Django project, when a user likes a Post the author of the post receives a notification that a post is liked and when this like is unliked the notification is deleted.我在我的 Django 项目中有一个通知系统,当用户喜欢帖子时,帖子的作者会收到一个帖子被喜欢的通知,当这个喜欢被取消时,通知将被删除。
Now I am trying to add a functionality in the notification system to get the total number of likes for all the posts created by an author and send a notification to the author when the total of likes reaches a certain number, instead I set a function that when a post reaches a certain number of likes a notification is sent.现在我想在通知系统中添加一个功能来获取作者创建的所有帖子的总点赞数,并在点赞总数达到一定数量时向作者发送通知,而不是我设置了一个 function当帖子达到一定数量的喜欢时,将发送通知。
I have been trying to accomplish this but I am getting several errors.我一直在努力做到这一点,但我遇到了几个错误。 I am going to comment on my trials so in the below codes.我将在下面的代码中评论我的试验。
As per the last trial I have made I am receiving: TypeError: total_likes_received() takes 1 positional argument but 2 were given
根据我所做的最后一次试验,我收到: TypeError: total_likes_received() takes 1 positional argument but 2 were given
Here is the Post model这是帖子 model
class Post(models.Model):
author = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='author')
num_likes = models.IntegerField(default=0, verbose_name='No. of Likes')
likes = models.ManyToManyField(User, related_name='liked', blank=True)
Here is the Like model这里是赞 model
class Like(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
value = models.CharField(choices=LIKE_CHOICES,
default='Like', max_length=8)
def total_likes_received(self):
return self.filter(post__author=Post.author).count()
#The number of Likes received from a post
def like_progress(sender, instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
if post.num_likes == 2:
notify = Notification(post=post, sender=sender,
user=post.author, notification_type=3)
notify.save()
post_save.connect(Like.like_progress, sender=Like)
-------------------------My Trial---------------------------
# def total_likes_received(user):
# return Like.objects.filter(post__author=Post.author).count()
#(This Trial returned TypeError: Field 'id' expected a number but got <django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x03F05DA8>.)
def total_likes_received(user):
return Like.filter(post__author=Post.author).count()
# The number of Likes received from all posts
def total_likes_progress(instance, *args, **kwargs):
like = instance
post = like.post
user = post.author
if like.total_likes_received(user) == 10:
notify = Notification(user=post.author, notification_type=3)
notify.save()
post_save.connect(Like.total_likes_progress, sender=Like)
-------------------------My Trial---------------------------
Here is the Notification model这是通知 model
class Notification(models.Model):
NOTIFICATION_TYPES=((1,'Like'),(2,'Comment'),(3,'Admin'))
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name="noti_post", blank=True, null=True)
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
user = models.ForeignKey(User, on_delete=models.CASCADE,related_name="noti_to_user")
notification_type= models.IntegerField(choices=NOTIFICATION_TYPES)
Do the following in your Like
model to solve the problem.在你的Like
model 中执行以下操作即可解决问题。
class Like(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
value = models.CharField(choices=LIKE_CHOICES,
default='Like', max_length=8)
def total_likes_received(self, user):
return Like.objects.filter(post__author=user).count()
def total_likes_progress(instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
if like.total_likes_received(user) == 10:
notify = Notification(sender=sender, user=post.author, notification_type=3)
notify.save()
post_save.connect(Like.total_likes_progress, sender=Like)
Here taking user
as an argument of total_likes_received
and filtering with it.这里将user
作为total_likes_received
的参数并使用它进行过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.