Python 使用调度uid防止重复信号

Python 使用调度uid防止重复信号,python,django,django-signals,Python,Django,Django Signals,我使用一个信号向作者发送一个通知,当一个新的活动,如用户点击一个类似的按钮发布文章 我使用这个信号的一个副作用是,当用户创建一个新帖子,并且用户喜欢这个帖子时,作者会收到2个通知,而不是1个 我已经搜索了几个答案,但它们影响了项目的其他方面,因此我在Django文档中发现,使用dispatch\u uid=“my\u unique\u identifier”可以解决这些问题。 我试过了,但没有成功 这就是: 这是Like模型。py 我的问题是如何正确使用dispatch\u uid=“My\u

我使用一个信号向作者发送一个通知,当一个新的活动,如用户点击一个类似的按钮发布文章

我使用这个信号的一个副作用是,当用户创建一个新帖子,并且用户喜欢这个帖子时,作者会收到2个通知,而不是1个

我已经搜索了几个答案,但它们影响了项目的其他方面,因此我在Django文档中发现,使用
dispatch\u uid=“my\u unique\u identifier”
可以解决这些问题。

我试过了,但没有成功

这就是:

这是Like模型。py

我的问题是如何正确使用dispatch\u uid=“My\u unique\u identifier”以避免重复信号?

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)
    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):
        like = instance
        if like.value=='Like':
            post = like.post
            sender = like.user
            dispatch_uid = "my_unique_identifier"
            notify = Notification(post=post, sender=sender, user=post.author, notification_type=1)
            notify.save()

    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()

    def like_progress(sender, instance, *args, **kwargs):
        like = instance
        post = like.post
        sender = like.user
        dispatch_uid = "my_unique_identifier"

        if post.num_likes == 2:
            notify = Notification(post=post, sender=sender, user=post.author, notification_type=3)
            notify.save()


# num_likes
post_save.connect(Like.like_progress, sender=Like, dispatch_uid="my_unique_identifier")

# Likes
post_save.connect(Like.user_liked_post, sender=Like,dispatch_uid="my_unique_identifier")
post_delete.connect(Like.user_unlike_post, sender=Like)