python - DateTimeField 中的 Django 和微秒差异

标签 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 = time 时,我得到 2017-04-29 09:36:14.895581。调用 offer.save 后,offer.time2017-04-29 09:36:14.895000。为什么 save() 不保留原始时间?

有什么想法吗?

提前致谢。

最佳答案

https://docs.mongodb.com/manual/reference/method/Date/

Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.

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

2017-04-29 09:36:14 将存储为 1493458574000 只留下 3 位小数来存储毫秒。添加毫秒后,该值将为 1493458574895。当将此值读回 DateTimeField 时,该值将为 2017-04-29 09:36:14.895000

关于python - DateTimeField 中的 Django 和微秒差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698285/

相关文章:

python - Pandas:当组中的值满足所需条件时从数据中删除组

Python - While循环不使用输出作为下一个循环的输入

django - 在 Amazon Elastic Beanstalk 上安装 Anaconda 以在 Django 应用程序中使用

MongoDB 索引 2 个字段并让索引用于第 3 个字段的搜索

不同数字长度的 CSV 数据的 Python bool 错误

python - 什么是 Ruby 中的 Python 文档字符串?

python - 如何在 Django 中渲染单个 View 时将一个模型中的字段用于另一个模型

python - 每月第一天的 Paypal 每月订阅计划设置并每月定期付款 - django python

node.js - 嵌入模式给出错误

javascript - 从 Mongo 的输出和嵌套元素中删除 _id