无法向用户发送信号

Can't send signals to the user

enter image description here我正在通过 Django 通知向用户发送通知.. 我有用户名正则表达式在 html 上工作所以任何 post 与 @username 它都会 post 和 html 是可链接的,所以点击@username 它将带他到用户名配置文件页面。现在我正在使用 django 信号来匹配用户名并打印出用户名。但是当我使用通知发送通知时。我找不到收件人(将收到通知的用户)。它给我一个错误 ValueError at /post/new/ Cannot assign "<_sre.SRE_Match object; span=(0, 4), match='@boy'>": "Notification.recipient" must be a "User" instance.

我的models.py:

class post(models.Model):
parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_pics', null=True, blank=True)
video = models.FileField(upload_to='post_videos', null=True, blank=True)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)

objects = postManager()

def __str__(self):
    return self.title

class Meta:
    ordering = ['-date_posted', 'title']

def get_absolute_url(self):
        return reverse ('blog-home')

def total_likes(self):
    return self.likes.count()


def post_save_receiver(sender, instance, created, *args,**kwargs):
    if created and not instance.parent:
        user_regex = r'@(?P<username>[\w.@+-]+)'
        m = re.search(user_regex, instance.content)
    if m:
        username = m.group("username")

        notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

post_save.connect(post_save_receiver, sender=post)

recipient可以Match对象:

notify.send(instance.author, <s>recipient=m</s>, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

您可以尝试通过以下方式获取 User 对象:

if m:
    try:
        <b>recipient = User.objects.get(username=m.group('username'))</b>
    except (User.DoesNotExist, User.MultipleObjectsReturned):
        pass
    else:
        notify.send(instance.author, <b>recipient=recipient</b>, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')