Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python DateTimeField中的Django和微秒差异_Python_Django_Mongodb_Django Models - Fatal编程技术网

Python DateTimeField中的Django和微秒差异

Python DateTimeField中的Django和微秒差异,python,django,mongodb,django-models,Python,Django,Mongodb,Django Models,我有一个计算给定日期时间的代码,如下所示 time = datetime.datetime.now() - datetime.timedelta(minutes=60) 然后,我使用time在offer对象中设置要保存在数据库中的字段。offer.time是一个offer模型实例;它被配置为: time = models.DateTimeField(blank=True, null=True) 但是,调用offer.save()时,offer.time会被更新。设置offer.time=t

我有一个计算给定日期时间的代码,如下所示

time = datetime.datetime.now() - datetime.timedelta(minutes=60)
然后,我使用
time
offer
对象中设置要保存在数据库中的字段。
offer.time
是一个
offer
模型实例;它被配置为:

time = models.DateTimeField(blank=True, null=True) 
但是,调用
offer.save()
时,offer.time会被更新。设置
offer.time=time
时,我得到
2017-04-29 09:36:14.895581
。调用
offer.save
后,
offer.time
2017-04-29 09:36:14.895000
。为什么
save()
不保留原始时间

有什么想法吗

提前谢谢。

在内部,日期对象存储为64位整数,表示 自Unix纪元(1970年1月1日)以来的毫秒数 结果表明,在距今约2.9亿年的时间范围内 过去和未来

这解释了为什么
2017-04-29 09:36:14.895581
变成
2017-04-29 09:36:14.895000


2017-04-29 09:36:14
将存储为
1493458574000
只剩下3个小数来存储毫秒。添加毫秒时,该值将为
1493458574895
。当该值读回
日期时间字段时,该值将为
2017-04-29 09:36:14.895000

@Grimmy这与此无关。作为记录,我使用的是MongoDB。我确实明白这可能与精度问题有关。啊。我猜可能是因为BSON Date是一个存储毫秒的64位整数。不确定最近是否发生了变化。添加mongodb标记:)我发现这可能确实是一个精度错误。不过,我不相信这个解释。根据您提供的解释,64位应该足以存储相关数据。你能澄清一下吗?没问题。我将添加更多信息。您可以创建自己的自定义字段,将64位整数存储为微秒?我不确定这在mongodb中的实际效果如何。我采用的解决方案基本上是将DateTimeField对象的微秒部分归零。这一级别的信息与我的应用程序无关。我不认为我把毫秒和微秒混淆了。在Python文档中(记住DateTimeField映射到Python中的datetime对象),存在一个
microseconds
属性,但不是一个毫秒属性。看见这些是我在原问题中提出的内容。