gpt4 book ai didi

用于处理简单用户通知系统的 Django 信号

转载 作者:行者123 更新时间:2023-12-05 00:55:59 29 4
gpt4 key购买 nike

我正在学习编码并拥有一个实时的 Django 项目来保持我的动力。在我的 Django 应用程序中,用户留下评论,而其他人则回复所述评论。

每次用户刷新他们的主页时,我都会计算他们是否收到了关于他们之前留下的评论的任何新回复,如果收到了则显示通知。

这不会扩展,因为如果用户留下了大量评论并因此收到大量回复,则计算时间比不创建内容的潜伏者要长。我想改善这些内容创作者的体验。

阅读 deeper into it ,我认为 Django 信号是要走的路。例如,每次留下新回复时,我都可以触发 post_save() 信号,该信号可以被接收并为用户更新通知。这样,通知将在留下回复时更新 - 这是应该的方式。

我将不得不重构我的代码,而且我对上述的实现细节仍然很模糊。谁能为我提供一个快速说明性示例,说明我如何完成上述操作?它会让我开始。

目前,用户回复处理在form_valid中处理。我的views.py class PublicreplyView(CreateView)中的CBV方法.我猜我可以在我的 CBV 中包含以下方法吗?
from django.db.models.signals import post_save
post_save.connect(form_valid, sender=User)

然后在其他地方我有一个不同的 CBV,一旦他们刷新主页,它就会为每个用户处理通知。我想我需要完全重写?就像我说的,我在这方面很模糊。

希望有人指导我如何处理一个说明性的简单示例。谢谢!

p.s.我在 Django < 1.8 上。

最佳答案

@Djizeus 在他的评论中给出了很好的概述,我会给你一个完整的例子。我将为通知创建单独的模型,并在创建评论时使用 post_save 信号创建它们。但是,我不会将此代码 Hook 以形成有效,而是在 Comment 模型上使用 post_save 信号。这样,您可以更清晰地分离代码。如果您决定拥有例如移动和桌面上的不同表单,或者如果您决定重构 PublicreplyView,您可能不必触摸信号代码。

当你保存评论时,Django 已经触发了 post_save 信号,所以你只需要听它们并处理它们。为此,请创建 signals.py 文件:

from django.dispatch import receiver
from django.db.models.signals import post_save
from .models import Comment, Notification

@receiver(post_save, sender=Comment)
def auto_create_notification(sender, instance, created, **kwargs):
if created:
# instance holds the new comment (reply), but you also have to fetch
# original comment and the user who created it
parent_comment = instance.parent_comment
parent_user = parent_comment.user
Notification.objects.create(user=parent_user,
comment=parent_comment,
type="reply_created")

要 Hook 此信号,​​您​​必须创建/编辑另外两个文件。将此添加到您的apps.py:
from django.apps import AppConfig

# change Comment with your app name
class CommentConfig(AppConfig):

name = 'comment'
verbose_name = 'Comment'

def ready(self):
import comment.signals

将此添加到您的 __init__.py :
default_app_config = 'comment.apps.CommentConfig'

关于您的主页 View 实现,这取决于您在主页上显示的内容。如果只显示通知,那么 Notification ListView 是一个不错的选择。如果您混合了不同的内容,那么我可能会使用 TemplateView 并获取您必须在其 get_context_data() 方法中显示的所有内容。

关于用于处理简单用户通知系统的 Django 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097255/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com