2013-03-19 7 views
8

私はdjangoプロジェクトの基本的なフィクスチャをいくつか設定しました。データベースに挿入されたレコードの1つは、次のようになります。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に深く隠されている、以下の引用:

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

と出力されました::

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

"2011-09-01T13:20:30+03: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'は、タイムゾーンを認識するための適切なタイムフォーマットではなく、 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

+0

生データのような音はタイムゾーンに固有なものではないので、 'USE_TZ = False'を試してみてください。 – danodonovan

+1

プロジェクト全体がタイムゾーンを意識している場合は、タイムゾーン対応の備品を用意する方が良いと思います。(' USE_TZ = True')とにかく、あなたのヒントは正確な答えに私を導いたので、私は+1、感謝を与える! – ducin

3

あなたはPyYamldatetime秒unserializingにバグがあるように思われるシリアル化するためにyamlを使用している場合:

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

シリアライザとしてjsonを使用したり、追加することができますいずれかを試してみてください.yamlファイルのdatetimeを引用符で囲みます。

関連する問題