RuntimeWarning:DateTimeField 收到一个天真的日期时间 [英] RuntimeWarning: DateTimeField received a naive datetime

查看:20
本文介绍了RuntimeWarning:DateTimeField 收到一个天真的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IPython 发送一封简单的邮件.我没有设置任何仍然出现此错误的模型.可以做什么?

I m trying to send a simple mail using IPython. I have not set up any models still getting this error. What can be done?

错误:/home/sourabh/Django/learn/local/lib/python2.7/site-packages/django/db/models/fields/init.py:827:RuntimeWarning:DateTimeField 收到一个幼稚的日期时间(2013-09-04 14:14:13.698105) 时区支持处于活动状态.运行时警告)

Error : /home/sourabh/Django/learn/local/lib/python2.7/site-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-09-04 14:14:13.698105) while time zone support is active. RuntimeWarning)

已尝试:第一步是将 USE_TZ = True 添加到您的设置文件并安装 pytz(如果可能).

Tried : The first step is to add USE_TZ = True to your settings file and install pytz (if possible).

错误改变:

(learn)sourabh@sL:~/Django/learn/event$ python manage.py shell
/home/sourabh/Django/learn/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:53: RuntimeWarning: SQLite received a naive datetime (2013-09-05 00:59:32.181872) while time zone support is active.
  RuntimeWarning)

推荐答案

问题不在于 Django 设置,而在于传递给模型的 日期.以下是时区感知对象的外观:

The problem is not in Django settings, but in the date passed to the model. Here's how a timezone-aware object looks like:

>>> from django.utils import timezone
>>> import pytz
>>> timezone.now()
datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC)

这是一个幼稚的对象:

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2013, 11, 20, 20, 9, 26, 423063)

因此,如果您在任何地方传递电子邮件日期(并且最终会到达某个模型),只需使用 Django 的 now().如果不是,则可能是现有软件包的问题,​​它获取没有时区的日期,您可以修补软件包、忽略警告或将 USE_TZ 设置为 False.

So if you are passing email date anywhere (and it eventually gets to some model), just use Django's now(). If not, then it's probably an issue with an existing package that fetches date without timezone and you can patch the package, ignore the warning or set USE_TZ to False.

这篇关于RuntimeWarning:DateTimeField 收到一个天真的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆