我打算通过在创建信号后断开视图中的信号来删除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)
return redirect('app:page2')
user 和 other_user 在此示例中是相同的用户
这将断开 和 之间的所有信号user
,other_user
我打算只断开这些用户之间为该特定对象创建的信号。
我已经查看了源代码,但我找不到如何能够做到这一点。
供您参考,GitHub链接为:https ://github.com/django-notifications/django-notifications
这也是该包中的信号文件:
''' 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'
])
编辑
这是我要实现的目标的更清晰示例:
在我的app/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'