django - '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'),
解决方案
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 %}
推荐阅读
- bluetooth - 我可以使用树莓派 3 使用 HID 配置文件通过蓝牙发送实时传感器数据吗?
- python - 以与使用元组相同的方式使用 ndarray 进行索引
- spring-boot - Liquibase Gradle 插件的 url 和 referenceUrl 似乎颠倒了
- react-native - 安全密码字段在发布时不受保护
- c++ - 定义子类构造函数时使用花括号调用基类构造函数?
- c# - 将预制件作为游戏对象的子对象统一生成
- directshow - GMFBridge DirectShow 滤镜 SetLiveTiming 效果
- java - 覆盖功能区服务器列表以从领事获取主机名列表
- javascript - Firebase 云消息传递每个 then() 都应该返回一个值或抛出 promise/always-return
- python-3.x - 如果响应条件,boto3 无法执行