Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django fixtures DateTimeField runtimeWarning

I've set some basic fixtures for my django project. One of the records inserted into the database looks like the following:

  {
    "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"
    }
  },

This is the model it corresponds to:

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

When inserting fixture records, I get the following warning:

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

I've got no idea what is wrong here. I tried to follow this blog post, but I do have pytz installed and I do have the USE_TZ=True option in my settings.py.

like image 776
ducin Avatar asked Mar 19 '13 17:03

ducin


3 Answers

Actually, the solution is hidden deeply in python docs, quote below:

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

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

Such fixtures are fully accepted, in my case it was:

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

and the output was:

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

Note, that '2013-03-16 17:41:28 UTC+0000' is not proper timezone aware datetime format and it will give you following error:

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."]
like image 142
ducin Avatar answered Oct 17 '22 23:10

ducin


Also if you are using yaml to serialize there seems to be a bug in unserializing datetimes in PyYaml:

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

Try either using json as serializer or you can add quotes around the datetime in the .yaml file.

like image 10
Emil Davtyan Avatar answered Oct 18 '22 00:10

Emil Davtyan


You should probably look closer at your created_at field (you do know about auto_now_add=True?).

I'm guessing at what you're using, so you could try something like

import datetime
from django.utils.timezone import utc

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

Or you could disable the timezone support by setting

USE_TZ = False

in your settings.py

Or you could make your unaware datetime aware

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)
like image 2
danodonovan Avatar answered Oct 18 '22 01:10

danodonovan