【问题标题】:How I can make notification system in Django by signals如何通过信号在 Django 中制作通知系统
【发布时间】:2021-03-07 17:51:18
【问题描述】:

我如何通过信号在 Django 中制作通知系统,其中任何用户添加喜欢到视频中都会向添加喜欢到他的视频的作者发送通知, 我尝试了一切,但没有任何东西对我有用

我的模特

class Video(models.Model):
    author = models.ForeignKey(Account, on_delete=models.CASCADE)
    video = models.FileField(upload_to='post-videos', validators=[validate_file_extension])
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    is_public = models.BooleanField(default=False)
    created_date = models.DateTimeField(auto_now_add=True)
    likes = models.ManyToManyField(Account, blank=True, related_name='likes', default=None)

信号文件

    @receiver(post_save, sender=Video.likes)
def update_likes(sender, instance, created, **kwargs):
    if created == False:
        instance.video.likes.save()
        print('like updated!')

post_save.connect(update_likes, sender=Video)

我尝试基于我的通知但我提交然后我通过信号尝试的通知模型

class Notification(models.Model):
NOTIFICATION_TYPE = ((1, 'like'))

video = models.ForeignKey("videos.Video", on_delete=models.CASCADE, related_name="noti_video", blank=True, null=True)
sender = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="noti_from_user")
user = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="noti_to_user")
notification_type = models.IntegerField(choices=NOTIFICATION_TYPE)
text_preview = models.CharField(max_length=90, blank=True)
date = models.DateTimeField(auto_now_add=True)
is_seen = models.BooleanField(default=False)

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    您正在使用 signals.py 文件? 文件是导入的吗? 尝试将您的 post_save 移至 models.py 文件... 需要将信号.py 文件导入为

    The right place to keep my signals.py file in a Django project

    django 不处理 signals.py 文件......如果它没有被导入......它不会在启动期间运行

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2015-12-19
      • 2012-02-11
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多