我不明白为什么我的django信号没有在数据库中创建对象

问题描述

我正在尝试构建一个通知应用程序,其中用户跟随另一个用户,他们在通知页面中收到通知,我现在的问题是我不明白为什么信号不向通知模型数据库中创建对象。请帮忙。这是代码

型号:

class Notification(models.Model):
    assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name="notifications_assigned_to_user")
    group = models.CharField(max_length=2,choices=notification_types,default='N')
    creation_date = models.DateTimeField(auto_Now_add=True)
    is_read = models.BooleanField(default=False)
    body = models.TextField(default='No Description')
    pk_relation = models.IntegerField(blank=True,null=True)

    def __str__(self):
        return f"For: {self.assigned_to.username} // id: {self.id}"

Signals.py:

@receiver(post_save,sender=Follow)
def create_notification(*args,**kwargs):
    follow = kwargs['instance']
    if kwargs['created']:
        # if task.created_by != task.assigned_to:
        if follow.follow_user != follow.user:
            Notification.objects.create(
                assigned_to = follow.user,group='NF',body=f"{follow.follow_user} followed you! ID: {follow.id}",pk_relation=follow.id
            )
    else:
        # if follow.created_by != task.assigned_to:
        if follow.follow_user != follow.user:
            if follow.follow_user != follow.old_instance.follow_user:
                Notification.objects.create(
                    assigned_to = follow.user,body=f"{follow.follow_user} unfollowed you.",pk_relation=follow.id
                )

@receiver(post_save,sender=Notification)
def send_notification_info(*args,**kwargs):
    if kwargs['created']:
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            f"notification_group_{kwargs['instance'].assigned_to.id}",{
                'type':'notification_info'
            }
        )

请帮助。我不太好,我觉得我遗漏了一些东西

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)