将 Django 项目中的信号通知限制为模型中的特定属性
Restricting Signal Notifications in a Django Project to specific Attribute in Model
我添加了一条通知,每当用户向 post 提交“赞”按钮时,被赞 post 的所有者都会收到通知。
我已将每个“赞”按钮的值选项设置为 Like
或 Unlike
所以现在我试图限制通知仅在点击的“赞”的值是 [=仅 14=]。
所以,现在在 Like 模型中,我添加了信号和条件,如果 Like.value== Like
信号激活但由于某种原因它不起作用,我不确定为什么。
我的问题是:
如何设置只在Like.value==Like
时发送通知信号?
这里是post models.py:
class Post(models.Model):
title = models.CharField(max_length=100, unique=True)
likes = models.ManyToManyField(User, related_name='liked', blank=True)
这里是赞models.py:
LIKE_CHOICES = (
('Like', 'Like'),
('Unlike', 'Unlike')
)
class Like(models.Model):
# To know Who liked
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)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.post}-{self.user}-{self.value}"
def user_liked_post(sender, instance, *args, **kwargs):
if Like.value=='Like':
like = instance
post = like.post
sender = like.user
notify = Notification(post=post, sender=sender, user=post.author, notification_type=1)
notify.save()
else:
None
def user_unlike_post(sender, instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
notify = Notification.objects.filter(post=post, sender=sender, user=post.author, notification_type=1)
notify.delete()
# Likes
post_save.connect(Like.user_liked_post, sender=Like)
post_delete.connect(Like.user_unlike_post, sender=Like)
这是通知models.py
class Notification(models.Model):
NOTIFICATION_TYPES=((1,'Like'),(2,'Comment'),(3,'Follow'))
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)
这里是views.py:
def ShowNotifications(request):
user=request.user
notifications= Notification.objects.filter(user=user).order_by('-date')
template= loader.get_template('notifications/notifications.html')
context= {
'notifications':notifications
}
return HttpResponse(template.render(context, request))
在 post_save 处理程序中,您比较如下 if Like.value=='Like':
但是 Like
是一个 class,其中 value
是一个字段,因此比较永远不会是 True
.
def user_liked_post(sender, instance, *args, **kwargs):
like = instance
if like.value=='Like':
post = like.post
sender = like.user
notify = Notification(
post=post, sender=sender, user=post.author, notification_type=1
)
notify.save()
我注意到的另一件事,我不确定影响是什么。 def user_liked_post
是模型上的一种方法。因此它期望的第一个参数是 self
。但是 post 保存处理程序将其用作 class 方法。但正因为如此 sender
可能会是 cls
,因此 Like
.
我添加了一条通知,每当用户向 post 提交“赞”按钮时,被赞 post 的所有者都会收到通知。
我已将每个“赞”按钮的值选项设置为 Like
或 Unlike
所以现在我试图限制通知仅在点击的“赞”的值是 [=仅 14=]。
所以,现在在 Like 模型中,我添加了信号和条件,如果 Like.value== Like
信号激活但由于某种原因它不起作用,我不确定为什么。
我的问题是:
如何设置只在Like.value==Like
时发送通知信号?
这里是post models.py:
class Post(models.Model):
title = models.CharField(max_length=100, unique=True)
likes = models.ManyToManyField(User, related_name='liked', blank=True)
这里是赞models.py:
LIKE_CHOICES = (
('Like', 'Like'),
('Unlike', 'Unlike')
)
class Like(models.Model):
# To know Who liked
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)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.post}-{self.user}-{self.value}"
def user_liked_post(sender, instance, *args, **kwargs):
if Like.value=='Like':
like = instance
post = like.post
sender = like.user
notify = Notification(post=post, sender=sender, user=post.author, notification_type=1)
notify.save()
else:
None
def user_unlike_post(sender, instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
notify = Notification.objects.filter(post=post, sender=sender, user=post.author, notification_type=1)
notify.delete()
# Likes
post_save.connect(Like.user_liked_post, sender=Like)
post_delete.connect(Like.user_unlike_post, sender=Like)
这是通知models.py
class Notification(models.Model):
NOTIFICATION_TYPES=((1,'Like'),(2,'Comment'),(3,'Follow'))
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)
这里是views.py:
def ShowNotifications(request):
user=request.user
notifications= Notification.objects.filter(user=user).order_by('-date')
template= loader.get_template('notifications/notifications.html')
context= {
'notifications':notifications
}
return HttpResponse(template.render(context, request))
在 post_save 处理程序中,您比较如下 if Like.value=='Like':
但是 Like
是一个 class,其中 value
是一个字段,因此比较永远不会是 True
.
def user_liked_post(sender, instance, *args, **kwargs):
like = instance
if like.value=='Like':
post = like.post
sender = like.user
notify = Notification(
post=post, sender=sender, user=post.author, notification_type=1
)
notify.save()
我注意到的另一件事,我不确定影响是什么。 def user_liked_post
是模型上的一种方法。因此它期望的第一个参数是 self
。但是 post 保存处理程序将其用作 class 方法。但正因为如此 sender
可能会是 cls
,因此 Like
.