問題描述:
使用save()想修改一個查詢集合中的值,修改後使用集合進行save()操作,結果報錯,mark下
代碼如下:
>>> from polls.models import Question
>>> q = Question.objects.get(pk=1)
>>> choice = q.choice_set.all()
>>> choice[0].choice_text
'test1'
>>> choice[0].choice_text = "橘子"
>>> choice.save()
報錯如下:
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'save'
原因分析:
choice是一個查詢結果集,沒有save的屬性,無法進行保存。