【问题标题】:'QuerySet' object has no attribute 'pk' , how I can get the Id of the post from home view?'QuerySet' 对象没有属性 'pk' ,如何从主视图获取帖子的 ID?
【发布时间】:2021-05-11 10:47:37
【问题描述】:

我如何在主页视图中获取对象的 pk,我尝试用帖子制作个人博客,并且此帖子包括 cmets,我尝试获取帖子 pk 以获取与该帖子相关的 cmets,但我失败了 首先,我尝试通过将其添加到 def home_view(request, pk) 来获取 pk,但主页中的此模板 www.mywebsite.com 这意味着我知道,我无法将 pk 传递给 url,所以这对我来说失败了

我的问题是如何从主页视图中获取帖子的 ID?

我的主页视图

post = []
for u in users:
  p = Account.objects.get(username=u)
            posts = p.post_set.all()

post.append(posts)
  if len(video):  
 video = sorted(chain(*video), reverse=True, key=lambda video:         video.created_date)

# here I tried to get the pk
comma = Post.objects.all()
our_post = Post.objects.get(pk=comma.pk)
comment = PostCommentIDF.objects.filter(post=our_post)

我的帖子模型

class Post(models.Model):
 author = models.ForeignKey(Account, on_delete=models.CASCADE)
    article = models.TextField(null=True, blank=True)
    photo_article = models.ImageField(max_length=255, upload_to=get_poster_filepath)
    created_date = models.DateTimeField(auto_now_add=True)

我的评论模型

class PostCommentIDF(MPTTModel):

post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='pos_com')
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='post_children')
author = models.ForeignKey(Account, on_delete=models.CASCADE)
content = models.TextField()
created_date = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)

主页视图网址

path('', home_screen_view, name='home'),

【问题讨论】:

  • comma = Post.objects.all() 返回QuerySet。它类似于列表或数组。属性pk 位于该“数组”中的每个项目上,而不是数组本身。
  • @zelenyjan 随时将其发布为答案..
  • @zelenyjan 有没有办法获取帖子项目pk并在评论中使用它comment = PostCommentIDF.objects.filter(post=post.pk)

标签: django django-models django-views django-forms django-templates


【解决方案1】:

真的不知道你的目标是什么让我试试......

comma = Post.objects.all() 返回QuerySet。它类似于列表或数组。属性 pk 位于该“数组”中的每个项目上,而不是数组本身。

如果您有 QuerySet(您的帖子列表),您可以循环访问它:

comma = Post.objects.all()  # returns all posts
for post in comma:  # now post is single post
    our_post = Post.objects.get(pk=post.pk)  # now you can call 'pk' because post is single post and not QuerySet... this line is not needed because you already have post
    comment = PostCommentIDF.objects.filter(post=post)  # now get comments for that one post

编辑: 如何在视图和模板中使用:

post_with_comments = []

comma = Post.objects.all()  # returns all posts
for post in comma:  # now post is single post
    our_post = Post.objects.get(pk=post.pk)  # now you can call 'pk' because post is single post and not QuerySet... this line is not needed because you already have post
    comment = PostCommentIDF.objects.filter(post=post)  # now get comments for that one post
    post_with_comments.append({"post": post, "comments": comment})

context = {"items": post_with_comments}
return context

然后在模板中循环所有post_with_comments

{% for item in items %}
  {{ item.post }}
  {{ item.comments.count }}
{% endfor %}

【讨论】:

  • 您好,谢谢您的回答。例如,我尝试了返回 cmets 的数量,并通过这种方式返回 0 {{comment.count}}
  • 尝试在comment = PostCommentIDF.objects.filter(post=post) 之后添加print(comment.count()) 如果您在数据库中有任何评论,您将看到至少一个帖子的计数不同,然后为 0。从您的评论中无法判断 0 是否可以
  • 这是有效的,但只是在 for 循环内打印时,但当它退出 for 循环时,它返回 0 我怎么能让它成为可能,因为我想将它传递给模板的上下文?谢谢你,我会认为这是公认的答案
  • @Ahmed 我更新的答案包括模板示例
  • 谢谢回复'set' object has no attribute '_mptt_meta' 但我认为这是与mptt cmets模型有关的问题
猜你喜欢
  • 2021-06-18
  • 2013-05-10
  • 2019-09-23
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 2020-10-06
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多