我有以下的模型和形式Django的DateTimeField字段和Postgres
class TravelShare(Share): # indirect derived via Share from models.Model
source = models.PointField(geography=True, srid=4326)
destination = models.PointField(geography=True, srid=4326)
departure = models.DateTimeField(default=timezone.now)
departure_delta = models.SmallIntegerField(default=60)
objects = TravelShareManager()
def __str__(self):
return self.id
class TravelShareForm(forms.ModelForm):
class Meta:
model = TravelShare
fields = ['source', 'destination', 'departure', 'departure_delta']
當
現在與
def test_TravelShareCreate(self):
source = Point(x=48.0, y=11.0, srid=4326)
destination = Point(x=48.0001, y=11.0001, srid=4326)
now = timezone.localtime(timezone.now())
print ("now : %s" % (now))
now_string = now.strftime('%m.%d.%Y %H:%M:%S')
form_data = {
'source': source,
'destination': destination,
'departure': now_string,
'departure_delta': 30,
}
form = TravelShareForm (form_data)
print (form.errors)
self.assertTrue(form.is_valid())
form.instance.creator = self.creator
result = form.save()
self.assertEqual(result.creator_id, self.creator.id)
self.assertEqual(result.source, source)
self.assertEqual(result.destination, destination)
result_dep = timezone.localtime(result.departure)
print("result_dep : %s" % (result_dep))
self.assertEqual(result.departure, now)
我結束了測試形式:2017年2月10日15 :49:21.935894 + 01:00
result_dep:2017-10-02 15:49:21 + 02:00
失敗
回溯(最近通話最後一個): 文件 「/home/michael/PycharmProjects/sharadar/main/tests/test_forms.py」,43行,在test_TravelShareCreate self.assertEqual(result.departure ,現在) AssertionError:datet [14個字符] 017,10,2,15,49,21,tzinfo = DstTzInfo'Eur [25個字符] DST)!= datet [14個字符] 017,2,10,15,49 ,21,935894,tzinfo = DstTzI [32個字符] STD
看來這導致日期時間具有不同的TZ或至少顯示+2代替1
感謝您的幫助
邁克爾
我找到了。數據庫的結果取整爲秒,因此15:49.21.93894 + 1.00變爲15:49:21 + 02:00 – mbieren