我正在尝试编辑现有的评论(即用新的评论替换旧的评论)。我的评论应用是django.contrib.comments。
new_comment = form.cleaned_data['comment']
#all of the comments for this particular review
comments = Comment.objects.for_model(Review).filter(object_pk=review_id)
print comments[0].comment
#'old comment'
comments[0].comment = new_comment
print comments[0].comment
#'old comment' is still printed为什么没有用新的评论更新评论?
谢谢。
编辑:调用comments[0].save(),然后调用print comments[0].comment,仍然打印'old comment'
发布于 2012-09-24 20:30:39
这与评论没有特别的关系。简单地说,每次切片时都会重新计算查询集。因此,您更改的第一个comments[0]与第二个不同-第二个将再次从数据库中获取。这将会起作用:
comments = Comment.objects.for_model(Review).filter(object_pk=review_id)
comment = comments[0]
comment.comment = new_comment现在,您可以根据需要保存或打印comment。
发布于 2012-09-24 19:25:51
您需要保存该值
comments = comments[0]
comments.comment = new_comment
comments.save()https://stackoverflow.com/questions/12564023
复制相似问题