Python ';用户';对象没有属性';获取受影响的用户';//关于涉外关系
我知道错误的意思,我知道为什么会发生这种情况。但我不知道如何解决它,实现我的目标。这是我正在做的 当其他用户对某个内容发表评论时,发布该内容的用户需要得到通知。我能够做到“当评论有回复时通知发表评论的人”。所以,我想我会对我遇到的问题做同样的事情。但是我得到了上面的错误。这是我的密码Python ';用户';对象没有属性';获取受影响的用户';//关于涉外关系,python,django,Python,Django,我知道错误的意思,我知道为什么会发生这种情况。但我不知道如何解决它,实现我的目标。这是我正在做的 当其他用户对某个内容发表评论时,发布该内容的用户需要得到通知。我能够做到“当评论有回复时通知发表评论的人”。所以,我想我会对我遇到的问题做同样的事情。但是我得到了上面的错误。这是我的密码 models.py征求意见 class Comment(models.Model): user = models.ForeignKey(MyProfile) parent = models.Fore
models.py
征求意见
class Comment(models.Model):
user = models.ForeignKey(MyProfile)
parent = models.ForeignKey("self", null=True, blank=True)
post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
@property
def get_origin(self):
return self.path
@property
def get_comment(self):
return self.text
@property
def is_child(self):
if self.parent is not None:
return True
else:
return False
def get_children(self):
if self.is_child:
return None
else:
return Comment.objects.filter(parent=self)
def get_affected_users(self):
"""
it needs to be a parent and have children,
the children, in effect, are the affected users.
"""
comment_children = self.get_children()
if comment_children is not None:
users = []
for comment in comment_children:
if comment.user in users:
pass
else:
users.append(comment.user)
return users
return None
获取受影响的用户
功能允许哪个用户获取通知。(创建回复的人不应得到通知,获得回复的人应得到通知)
在myviews.py中
def comment_create_view(request):
form = CommentForm(request.POST)
if form.is_valid():
comment_text = form.cleaned_data['comment']
if parent_comment is not None:
# parent comments exists
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=parent_comment.get_origin,
text=comment_text,
post = post,
parent=parent_comment
)
affected_users = parent_comment.get_affected_users()
notify.send(
MyProfile.objects.get(user=request.user),
action=new_comment,
target=parent_comment,
recipient=parent_comment.user,
affected_users = affected_users,
verb='replied to')
#the above code gives reply-comment notification makes possible, the below code is the one I'm struggling
else:
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=origin_path,
text=comment_text,
post = post
)
这一行导致了问题,可能是因为我的帖子模型中没有get\u impacted\u user
函数,但在comment模型中,我有一个带有foreignkey的Post字段。那么从技术上讲,我不能使用这个函数吗
affected_users = [post.moderator.get_affected_users()]
notify.send(
MyProfile.objects.get(user=request.user),
recipient = MyProfile.objects.get(user=request.user),
action=new_comment,
target = new_comment.post,
affected_users = affected_users,
verb='commented on')
我在评论中有get\u impacted\u用户
,在评论中我有与帖子有外键关系的帖子,在帖子中我有版主。所以我想我可以用get\u impact\u用户
作为post.版主
。。。但我想不是,那我该怎么做呢
如果问题不清楚,请原谅。任何帮助都将不胜感激。我假设Post.版主是一个FK to MyProfile,与Comment.user一样。第二,我想你要做的是,当在一个Post对象上创建了一个新的顶级评论,或者一个没有parent
设置的评论时,通知主持人。在这种情况下,您的行可能只是受影响的\u用户=[post.moderator]
版主。获得受影响的\u用户
。我假设,版主
是一个用户,因此实际上没有受影响的用户()
。我在评论中有受影响的用户,在评论中我有一篇与帖子有外键关系的帖子,在帖子中我有版主。所以我想我可以用博文版主来吸引受影响的用户……但我想不行,那我该怎么做呢?我不知道;这取决于你到底想要什么。只需从帖子或其他地方获取评论:post.comment.get\u infected\u users()
,或者迭代帖子中的所有评论。或者为您的帖子模型创建一个新函数。然后我会收到我所做评论的通知。