首页 > 解决方案 > 'QuerySet' object has no attribute 'pk' , how I can get the Id of the post from home view?

问题描述

How I can get the pk of objects in home view, I try to make personal blog with posts and this posts include comments, I tried to get the post pk in order to get the comments that related to this post but I failed Firstly I tried to get th pk by add it in def home_view(request, pk) but this template in home page www.mywebsite.com that's mean as I know, I cann't pass pk to the url so that's failed with me

My qustion is how I can get the Id of the post from home view?

My home View

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)

My Post Model

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)

My Comment Model

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)

The Home View Url

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

标签: djangodjango-modelsdjango-viewsdjango-formsdjango-templates

解决方案


Really not sure what's your goal by let me try it...

comma = Post.objects.all() returns QuerySet. It's something like list or array. Attribute pk is on each item in that 'array' not on array itself.

If you have QuerySet (list of your posts) you can loop over it:

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

EDIT: How to use in view and template:

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

and then in the template you loop over all post_with_comments

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

推荐阅读