我創建testapp和測試您的問題
models.py
from django.db import models
class Test2(models.Model):
date = models.DateField(editable=False)
class Test(models.Model):
test2 = models.ForeignKey(Test2)
tests.py
from django.test import TestCase
from models import Test, Test2
from datetime import datetime, date
class SimpleTest(TestCase):
def test_field_update(self):
old_date = date(2012,11,11)
current_date = datetime.today().date()
t2 = Test2(date = old_date)
t2.save()
t1 = Test(test2 = t2)
t1.save()
for test_obj in Test.objects.all():
test2_obj = test_obj.test2
test2_obj.date = current_date
test2_obj.save()
test_obj.save()
t2 = Test2.objects.all()[0]
self.assertEqual(t2.date, current_date)
運行測試
..> manage.py test testapp
爲別名'default'創建測試數據庫...。 ------------------------------------------------- ---------------------冉1次測試中0.016s
OK銷燬別名 '默認' 的測試數據庫...
所以你這個按預期工作,而不是像你所發佈的那樣。
你可以顯示讓你相信它沒有改變的測試代碼嗎? – KayEss
SELECT * FROM app_test2; :) – marxin
刪除行'test_obj.save()' – scytale