'QuerySet' 对象不支持项目分配
'QuerySet' object does not support item assignment
class PostDetailView(DetailView):
model = Post
template_name = 'detail.html'
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
instance = Post.objects.get(pk=self.kwargs.get('pk'))
user = instance.post_user
context['comments'] = Comment.objects.filter(comment_post=instance.pk)
context['comments']['profile'] = Profile.objects.get(user=user)
return context
这是我目前的看法。当我使用该代码时,出现此错误 'QuerySet' object does not support item assignment。如何正确附加下面的行?
context['comments']['profile'] = Profile.objects.get(user=user)
问题是 context['comments']
的值不是字典而是 QuerySet 对象。
所以你不能这样做:
context['comments']['profile'] = Profile.objects.get(user=user)
.
也许您可以将与 Profile 模型的关系添加到 Comment 模型中,例如:
class Comment(models.Model):
profile = models.ForeignKey(Profile, ...)
...
这样您就可以访问发表评论的个人资料的值。
class PostDetailView(DetailView):
model = Post
template_name = 'detail.html'
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
instance = Post.objects.get(pk=self.kwargs.get('pk'))
user = instance.post_user
context['comments'] = Comment.objects.filter(comment_post=instance.pk)
context['comments']['profile'] = Profile.objects.get(user=user)
return context
这是我目前的看法。当我使用该代码时,出现此错误 'QuerySet' object does not support item assignment。如何正确附加下面的行?
context['comments']['profile'] = Profile.objects.get(user=user)
问题是 context['comments']
的值不是字典而是 QuerySet 对象。
所以你不能这样做:
context['comments']['profile'] = Profile.objects.get(user=user)
.
也许您可以将与 Profile 模型的关系添加到 Comment 模型中,例如:
class Comment(models.Model):
profile = models.ForeignKey(Profile, ...)
...
这样您就可以访问发表评论的个人资料的值。