编辑Django评论 [英] Editing a Django Comment
问题描述
我正在尝试修改现有评论(即用新的评论替换旧评论)。我的评论应用是django.contrib.comments。
I'm attempting to edit an existing comment (i.e. replace old comment with a new one). My comments app is 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
为什么评论不会被新评论更新?
Why is the comment not being updated with the new comment ?
谢谢。
编辑:
调用评论[0] .save()
然后打印评论[0] .comment
,仍然打印'旧评论'
推荐答案
这与具体的评论无关。简单来说,每次切片时都会重新评估查询结果。所以你改变的第一个评论[0]
与第二个不一样 - 第二个是从数据库中再次获取的。这将工作:
This isn't to do with comments specifically. It's simply that querysets are re-evaluated every time you slice. So the first comments[0]
, which you change, is not the same as the second one - the second one is fetched again from the database. This would work:
comments = Comment.objects.for_model(Review).filter(object_pk=review_id)
comment = comments[0]
comment.comment = new_comment
现在可以保存或打印 comment
必要时。
Now you can save or print comment
as necessary.
这篇关于编辑Django评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!