【发布时间】:2012-11-29 00:03:27
【问题描述】:
我有这样的情况:
型号:
class Test2(models.Model) {
date = models.DateField(editable=False)
}
class Test(models.Model) {
test2 = models.ForeignKey(Test2)
}
行动:
for test_obj in Test.objects.all():
test2_obj = test_obj.test2
test2_obj.date = date.today()
test2_obj.save()
test_obj.save()
在该操作之后,test2_obj 的日期不会更改。为什么? test2_obj = test_obj.test2 是副本吗?
【问题讨论】:
-
你能展示让你相信它没有改变的测试代码吗?
-
从 app_test2 中选择 *; :)
-
删除
test_obj.save()这一行 -
在实际情况下我需要那条线。有问题吗?也许我应该在第一次保存之前移动它?
标签: python django foreign-keys models django-queryset