Python 通过断开Django通知hq中的信号来删除Django通知
我打算在创建信号后,通过断开视图中的信号来删除django notifications hq包中的通知: 例如:Python 通过断开Django通知hq中的信号来删除Django通知,python,django,django-views,django-signals,django-notification,Python,Django,Django Views,Django Signals,Django Notification,我打算在创建信号后,通过断开视图中的信号来删除django notifications hq包中的通知: 例如: def view1: notify.send(sender=user, recipient=other_user, verb=message, target=object) return redirect('app:page') def view2: notify.disconnect(sender=user, reciever=other_user)
def view1:
notify.send(sender=user, recipient=other_user, verb=message, target=object)
return redirect('app:page')
def view2:
notify.disconnect(sender=user, reciever=other_user)
return redirect('app:page2')
在本例中,user和其他_user是相同的用户
这将断开用户
和其他用户
之间的所有信号,我只想断开这些用户之间为特定对象创建的信号
我已经研究了源代码,但我找不到如何做到这一点
供您参考,GitHub链接为:
此外,这是该软件包中的:
''' Django notifications signal file '''
# -*- coding: utf-8 -*-
from django.dispatch import Signal
notify = Signal(providing_args=[ # pylint: disable=invalid-name
'recipient', 'actor', 'verb', 'action_object', 'target', 'description',
'timestamp', 'level'
])
编辑
下面是一个更明确的例子,说明我正在努力实现的目标:
在myapp/views.py中
我有:
def post_like(request, id):
post = get_objects_or_404(Post, id=id)
if request.user in post.likes:
post.likes.remove(request.user)
# remove notification sent to post.author here
elif request.user not in post.likes:
post.likes.add(request.user)
notify.send(sender=request.user, recipient=post.author, verb="Liked your post", target = post)
"""
Continue other functions here
"""
如何连接和断开此视图的信号?我在文档中找不到示例
编辑
我正在尝试根据通知模型字段获取特定通知:
request.user.notifications.get(actor_content_type__model='Profile', actor_object_id=request.user.id, target_content_type__model='home.Post', target_object_id=post.id, recipient=post.author, verb=message)
我有一个错误:
raise self.model.DoesNotExist(
main.models.Notification.DoesNotExist: Notification matching query does not exist.
在查看更多信息后,我注意到:
AttributeError: 'Notification' object has no attribute 'actor_content_type__model'
你的问题应该是关于信号,而不是通知,而是关于如何和何时调用page2或者更确切地说是view2。那么,你建议我如何在视图中随时断开带有这些特定参数的信号。View2可以在我的应用程序中的任何时间点调用,我希望能够随时删除这些对象创建的信号@解锁查看此信息#通知最好在单独的信号中完成。通过这种方式,您可以断开该处理程序的连接。def my_handler(sender,instance,created,**kwargs):notify.send(instance,verb='was saved')request_finished.connect(my_handler),request_finished.disconnect(my_handler)
试试看。@unlock谢谢您的信息,但很遗憾,我做不到,我找不到断开特定通知参数处理程序连接的方法。我举了一个例子,当你说断开通知时,我觉得现在我是如何发送通知的。您的意思是,您希望已创建的通知不会为用户显示。我希望您知道notify.send实际上在数据库中创建了一个新的通知实例。在这种情况下,可以将特定通知标记为已读@ashes999