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

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

问题描述

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

<块引用>

错误:/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) 时区支持处于活动状态.运行时警告)

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

错误已更改:

(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 收到一个天真的日期时间 (2013-09-05 00:59:32.181872) 时区支持处于活动状态.运行时警告)

解决方案

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

<预><代码>>>>从 django.utils 导入时区>>>进口pytz>>>时区.now()datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC)

这是一个简单的对象:

<预><代码>>>>从日期时间导入日期时间>>>日期时间.现在()datetime.datetime(2013, 11, 20, 20, 9, 26, 423063)

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

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?

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)

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

Error changed:

(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)

解决方案

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)

And here's a naive object:

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

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天全站免登陆