热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Timezone.now()vsdatetime.datetime.now()-Timezone.now()vsdatetime.datetime.now()

WhenshouldIbeusingdjangostimezone.now()andwhenshouldIbeusingpythongsdatetime.datetim

When should I be using django's timezone.now() and when should I be using pythong's datetime.datetime.now().

我何时应该使用django的timezone.now()以及何时应该使用pythong的datetime.datetime.now()。

For example, in the following INSERT which would make more sense?

例如,在下面的INSERT中会更有意义吗?

- Product.objects.create(title='Soap', date_added=datetime.datetime.now())
- Product.objects.create(title='Soap', date_added=timezone.now())

Is there a rule of thumb on when to use each?

是否有关于何时使用每种方法的经验法则?

1 个解决方案

#1


12  

Just always use timezone.now(). Django now has timezone support which requires timezone 'aware' datetime objects. datetime.now() will return a timezone naive object, whereas timezone.now() will return a timezone aware object.

只需使用timezone.now()。 Django现在有时区支持,需要时区“识别”日期时间对象。 datetime.now()将返回一个时区天真对象,而timezone.now()将返回一个时区感知对象。


推荐阅读
author-avatar
lajallan608
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有