简体   繁体   English

“用户”对象没有关于外键关系的属性“ get_affected_users” //

[英]'User' object has no attribute 'get_affected_users'// about foreignkey relation

I know what the error means, I have a clue why this is happening. 我知道错误的含义,我有一个线索为什么会这样。 But I don't know how to fix it and achieve my goal. 但是我不知道如何解决它并实现我的目标。 Here's what I'm doing. 这就是我在做什么。

Users who posted something need to get notification when some other user posts comment on that something. 发布其他内容的用户需要在其他用户发布有关该内容的评论时获得通知。 I was able to do "notifying the person who commented when there's a reply to that comment". 我能够做到“在收到评论的回复时通知发表评论的人”。 So, I thought I would do the same thing for the problem I'm having. 所以,我想我会为遇到的问题做同样的事情。 But I got the above error. 但是我得到了上面的错误。 Here's my code. 这是我的代码。

models.py for comment 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

get_affected_user function allows which user to get notification. get_affected_user函数允许哪个用户获得通知。 (the person who created the reply shouldn't get notification, the person who got the reply should get notified). (创建回复的人不会收到通知,获得回复的人应该收到通知)。

In my views.py 在我的views.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
                )

This line is causing the problem, probably because I don't have get_affected_user function in my Post model, but in comment model I have post field which has a foreignkey with Post. 这行引起了问题,可能是因为我的Post模型中没有get_affected_user函数,但是在注释模型中,我的post字段具有与Post的外键。 so technically can't I use this function? 所以从技术上讲我不能使用此功能吗?

            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')

I have get_affected_users in Comment, and in Comment I have post which has foreignKey relationship with Post and in Post I have moderator. 我在Comment中有get_affected_users ,在Comment中有与Post具有ForeignKey关系的帖子,在Post中有主持人。 So I thought I could've used get_affected_users for post.moderator ... But I guess not, how do I do it then? 因此,我认为我本可以将get_affected_users用于post.moderator ...但我猜不是,那我该怎么办?

Please excuse me if the question isn't clear. 如果问题不清楚,请原谅。 Any help will be highly appreciated. 任何帮助将不胜感激。

I'm going to assume that Post.moderator is a FK to MyProfile just the same as Comment.user. 我将假定Post.moderator是MyProfile的FK,与Comment.user相同。 Second, I'm going to guess that what you're trying to do is notify the moderator when a new top-level comment is created on a Post object, or a Comment with no parent set. 其次,我猜想您要执行的操作是在Post对象或未设置parent的Comment上创建新的顶级注释时通知主持人。 In that case, your line should probably just be affected_users = [post.moderator] . 在这种情况下,您的行可能只应该是affected_users = [post.moderator]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM