我有以下模型和表格
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-02-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,now) AssertionError:datet [14 chars] 017,10,2,15,49,21,tzinfo = DstTzInfo' Eur [25 chars] DST)!= datet [14 chars] 017,2,10,15,49, 21,935894,tzinfo = DstTzI [32 chars] STD
似乎结果DateTime具有不同的TZ或者至少显示+2而不是+1
感谢您的帮助
迈克尔
答案 0 :(得分:0)
数据库的结果四舍五入为秒,因此15:49.21.93894 + 1.00变为15:49:21 + 02:00
除了strftime格式字符串应该是:('%d。%m。%Y%H:%M:%S')而不是('%m。% d。%Y%H:%M:%S')
我的测试现在是这样的:
class TestTravelShareForm(TestCase):
def setUp(self):
self.creator = ShrUser.objects.create_user(username="foo", email="hallo.ich@localhost.de", password="bla")
self.source = Point(x=48.0, y=11.0, srid=4326)
self.destination = Point(x=48.0001, y=11.0001, srid=4326)
self.now = timezone.now()
self.now_string = self.now.strftime('%d.%m.%Y %H:%M:%S')
def test_TravelShareCreate(self):
form_data = {
'source': self.source,
'destination': self.destination,
'departure': self.now_string,
}
form = TravelShareForm (form_data)
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, self.source)
self.assertEqual(result.destination, self.destination)
result_dep = result.departure.strftime('%d.%m.%Y %H:%M:%S')
self.assertEqual(result_dep, self.now_string)
答案 1 :(得分:0)
数据库的结果四舍五入为秒,因此15:49.21.93894 + 1.00变为15:49:21 + 02:00
这是完全错误的。在将datetime对象转换为字符串now.strftime('%m.%d.%Y %H:%M:%S')
期间,您已经删除了微秒部分。
而且,+02:00
是datetime对象的时区utc偏移量。这不是一个圆形的秒或其他什么。
真正的问题在于你的strftime字符串格式。今天,您的时区可能有夏令时。但是,当您将错误的格式化日期时间字符串传递给表单时,django表单将其解析为2017-10-02(2017年10月2日),这是您所在时区的标准时间。所以,这就是为什么你有不同的utc偏移:+01:00和+02:00。