2013-03-19 73 views
8

我为我的django项目设置了一些基本的灯具。一个插入到数据库中的记录如下所示:django灯具DateTimeField runtimeWarning

{ 
    "model": "articles.article", 
    "pk": 1, 
    "fields": { 
     "title": "Blackened Recordings Launches", 
     "headline": "we're so psyched about our new adventure", 
     "content": "<p>We like to make it a point here not to bore you with the not-so-exciting business aspects of making and sharing music, but we're so psyched about our new adventure that we just had to tell you about it as we officially launch our very own record label, Blackened Recordings.</p><p>Some of you, who have followed along throughout the years, are aware that in 1994 we renegotiated our contract with the Warner Music Group, which resulted in a joint venture with our record company for releasing all of our recordings including long form videos. Per that agreement, as of today we have taken ownership of all of our master recordings and Blackened Recordings will be the home of all of our current albums and videos along with all future releases including the December 10 release of the \"Quebec Magnetic\" DVD and Blu-ray.</p><p>You may have heard us say it once or twice or a thousand times before, but it's always been about us taking control of all things 'Tallica to give you 110% on every single level every single time. Forming Blackened Recordings is the ultimate in independence, putting us in the driver's seat of our own creative destiny. We're looking forward to making more music and getting it all out to you in our own unique way.</p>", 
     "image": "examples/slide-03.jpg", 
     "active": 1, 
     "created_at": "2013-03-16 17:41:28" 
    } 
    }, 

这是它的对应型号:

class Article(models.Model): 
    """News article, displayed on homepage to attract users""" 
    class Meta: 
     db_table = 'article' 
    title = models.CharField(max_length=64) 
    headline = models.CharField(max_length=255) 
    content = models.TextField() 
    image = models.ImageField(upload_to = 'articles/', null=True, blank=True) 
    active = models.BooleanField() 
    created_at = models.DateTimeField() 
    def __unicode__(self): 
     return self.title 

当插入固定装置的记录,我得到以下警告:

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-03-16 17:41:28) while time zone support is active. 
    RuntimeWarning) 

我不知道这里出了什么问题。我试图按照this blog post,但我确实安装了pytz,我的settings.py中有USE_TZ=True选项。

回答

14

实际上,该解决方案在python docs深深地隐藏起来,如下报价:

When serializing an aware datetime, the UTC offset is included, like this:

"2011-09-01T13:20:30+03:00"

这种灯具完全接受,在我的情况是:

"2013-03-16T17:41:28+00:00" 
"2013-03-17T23:36:12+00:00" 
"2013-03-18T13:19:37+00:00" 

输出功率为:

$ ./manage.py loaddata articles/fixtures/initial_data.json 
Installed 3 object(s) from 1 fixture(s) 

请注意,'2013-03-16 17:41:28 UTC+0000'是不适当的时区知晓日期时间格式,它会给你followi NG错误:

DeserializationError: Problem installing fixture 'articles/fixtures/initial_data.json': [u"'2013-03-16 17:41:28 UTC+0000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."] 
2

你应该仔细看看你的created_at字段(你的know aboutauto_now_add=True?)。

我猜你正在使用的,所以你可以尝试像

import datetime 
from django.utils.timezone import utc 

Article.created_at = datetime.datetime.utcnow().replace(tzinfo=utc) 

或者,你可以通过在你的settings.py

设置

USE_TZ = False 

禁用时区支持

或者你可以让你不知道日期时间知道

import datetime 
import pytz 
utc=pytz.UTC 

# where ever you get your datetime from 
unaware = datetime.datetime(2013,3,16,17,41,28,0) 

now_aware = utc.localize(unaware) 
+0

我正在通过'./manage.py loaddata path_to_file'或者'./manage.py syncdb'(只有自动的东西)来加载灯具,而不是直接在python代码中。它对你的回复有什么影响吗? – ducin 2013-03-20 09:57:35

+0

因此,听起来像你的原始数据不是特定时区 - 所以请尝试'USE_TZ =假' – danodonovan 2013-03-20 10:09:05

+1

我认为如果整个项目是时区感知('USE_TZ = True'),准备时区感知的灯具更好。无论如何,你的提示让我得到了准确的答案,所以我给+1,谢谢! – ducin 2013-03-20 14:14:43

3

此外,如果你正在使用yaml序列化,似乎是在反序列化datetime只虫子在PyYaml

https://code.djangoproject.com/ticket/18867

尝试或者使用json作为串行或您可以添加.yaml文件中的日期时间引号。