django -- while time zone support is active

一、先看报错:

django 在处理datetime类型的的报错
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1451: RuntimeWarning: DateTimeField Person.birthday received a naive datetime (2017-12-02 08:02:02.776941) while time zone support is active.

二、问题分析:

  1、默认情况下django中对时区的配置是UTC的、然而datetime中并没有明确指点时区、这样就冲突了。

三、解决方案1:

  使用django提供的timezone类

import django
import pytz
from django.utils import timezone
from datetime import datetime django.setup()
from polls.models import Person p=Person()
p.name='welson'
p.birthday=timezone.now()
p.save()

三、timezone和python内置的datetime的不同:

import django.timezone
from datetime import datetime datetime.now()
datetime.datetime(2017, 12, 2, 8, 37, 11, 354441) timezone.now()
datetime.datetime(2017, 12, 2, 8, 36, 49, 310501, tzinfo=<UTC>)

----

上一篇:How are you vs How are you doing


下一篇:js实现无刷新表单提交文件,将ajax请求转换为form请求方法