Python ';邮政';对象没有属性';注释集';由post.comment\u set.all()引起,当comment\u set是外键时意味着什么?

Python ';邮政';对象没有属性';注释集';由post.comment\u set.all()引起,当comment\u set是外键时意味着什么?,python,django,Python,Django,我正在学习一些教程,但是当指导老师说我应该把一个外键从一个帖子放到另一个帖子上以获得评论时,我没有理解。我没有评论集,而是评论集 #path = request.get_full_path() #comments = Comment.objects.filter(path=path) 现在我得到了孩子们的评论,而我不应该得到孩子们的评论。当我做post.comment\u set.all(),而不是上面两行时,我不做任何注释,也不出现此错误;'Post对象没有属性“comment\u set”

我正在学习一些教程,但是当指导老师说我应该把一个外键从一个帖子放到另一个帖子上以获得评论时,我没有理解。我没有评论集,而是评论集

#path = request.get_full_path()
#comments = Comment.objects.filter(path=path)
现在我得到了孩子们的评论,而我不应该得到孩子们的评论。当我做
post.comment\u set.all(),
而不是上面两行时,我不做任何注释,也不出现此错误;'Post对象没有属性“comment\u set” . 有人能澄清一下如何
comments=post.comment\u set.filter吗
有效吗?这是我的全部代码

models.py

class Comment(models.Model):
    user = models.ForeignKey(MyProfile)
    parent = models.ForeignKey("self", null=True, blank=True)
    path = models.CharField(max_length=350)
    post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
    text = models.TextField()
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    active = models.BooleanField(default=True)

    objects = CommentManager()

    class Meta:
        ordering = ['-timestamp']

    def __unicode__(self):
        return self.text


    def get_absolute_url(self):
        return reverse('comment_thread', kwargs={"id": self.id})

    @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)
还有我的观点

#for single-post page
def post(request, slug):
        user = get_object_or_404(User,username__iexact=request.user)
        try:
            profile = MyProfile.objects.get(user_id=request.user.id)
            # if it's a OneToOne field, you can do:
            # profile = request.user.myprofile
        except MyProfile.DoesNotExist:
            profile = None


        post = get_object_or_404(Post, slug=slug)
        post.views += 1  # increment the number of views
        post.save()      # and save it

        #path = request.get_full_path()
        #comments = Comment.objects.filter(path=path)
        comments = post.comment_set.all()
        comment_form = CommentForm(request.POST or None)
        for c in comments:
                c.get_children()

        context_dict = {
            'post' :post,
            'profile' :profile,
            'comments' : comments,
            'comment_form':comment_form
        }
        return render(request, 'main/post.html', context_dict)
Django支持所谓的“向后关系”,即从实体对象向后访问子实体。假设我们有下一个型号:

class Post(models.Model):
    #some fields

class Comment(models.Model):
    post = models.ForeignKey(Post)
然后Django给您提供了通过
post.Comment\u set
语句访问注释的默认设置。但是,如果您想在外键的注释中写入相关的注释名选项,它会将此默认注释集更改为。。。您在相关名称选项中所写的内容。你可以看看这个。如果你写了

post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
这意味着您必须通过以下方式访问您的评论:

post.commented_post.all()
希望有帮助

Django支持所谓的“向后关系”,即从实体对象向后访问子实体。假设我们有下一个型号:

class Post(models.Model):
    #some fields

class Comment(models.Model):
    post = models.ForeignKey(Post)
然后Django给您提供了通过
post.Comment\u set
语句访问注释的默认设置。但是,如果您想在外键的注释中写入相关的注释名选项,它会将此默认注释集更改为。。。您在相关名称选项中所写的内容。你可以看看这个。如果你写了

post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
这意味着您必须通过以下方式访问您的评论:

post.commented_post.all()

希望有帮助

您编写了
post=models.ForeignKey(post,null=True,blank=True,related\u name=“commented\u post”)
。这意味着您可以通过执行
post.commented\u post.all()
从post获取所有评论。如果您没有相关的\u名称,那么它将是comment\u set.all()。这是反向关系,[读这里]()@AlexanderB。啊哈,这是有道理的…但为什么我得到一个错误,它没有属性注释集?这是默认的向后关系。例如,您有类A和类B,它们的foreignKey指向A。要从A的实例中获取其对象B,您将使用
A.B_集
,这意味着根据关系,如果您有一些类实体,那么它将是“实体_集”。当您更改默认名称时,django不会创建属性comment\u set,而是创建commented\u post。这就是为什么你有“无属性”错误。非常感谢,这是答案太好了!我明天会加上答案,如果你愿意,你可以自己写下来接受!您编写了
post=models.ForeignKey(post,null=True,blank=True,related\u name=“commented\u post”)
。这意味着您可以通过执行
post.commented\u post.all()
从post获取所有评论。如果您没有相关的\u名称,那么它将是comment\u set.all()。这是反向关系,[读这里]()@AlexanderB。啊哈,这是有道理的…但为什么我得到一个错误,它没有属性注释集?这是默认的向后关系。例如,您有类A和类B,它们的foreignKey指向A。要从A的实例中获取其对象B,您将使用
A.B_集
,这意味着根据关系,如果您有一些类实体,那么它将是“实体_集”。当您更改默认名称时,django不会创建属性comment\u set,而是创建commented\u post。这就是为什么你有“无属性”错误。非常感谢,这是答案太好了!我明天会加上答案,如果你愿意,你可以自己写下来接受!嘿,老兄,你能帮我解决这个问题吗,我已经有两天的问题了…嘿,老兄,你能帮我解决这个问题吗,我已经有两天的问题了。。。