Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Naive datetime while time zone support is active (sqlite)

I'm going around in circles on this on and need some help. I continue to get a naive timezone warning. Not sure what I am doing wrong! Arg.

Here is the warning:

/django/db/models/fields/__init__.py:1222: RuntimeWarning: DateTimeField Video.modified received a naive datetime (2014-10-07 00:00:00) while time zone support is active.
  RuntimeWarning)

Here is the model code (redacted somewhat):

from django.db import models
from django.utils import timezone

class ItemBase(models.Model):
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField(editable=False)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        """Updates timestamps on save"""
        if not self.id:
            self.created = timezone.now()
        self.modified = timezone.now()
        return super(ItemBase, self).save(*args, **kwargs)

class Video(ItemBase):
    pass

And the relevant (I think) part of my settings file:

TIME_ZONE = 'UTC'
USE_TZ = True

Is this a sqlite issue (am still testing things)? Or am I missing something fundamental here? I've read up on it here and here and, of course, at the docs here. But I am stumped. Thanks.

edit: added test that throws the error

Am getting the error when I run my tests ... I left the redacted stuff in there but you should get the idea:

from django.test import TestCase
from django.contrib.auth import get_user_model

from video.models import Video, VideoAccount

class VideoTestCase(TestCase):

    def setUp(self):
        user = get_user_model().objects.create_user(
            username='jacob', email='[email protected]', password='top_secret')
        self.video_account = VideoAccount.objects.create(
            account_type=1, account_id=12345, display_name="Test Account" )
        self.pk1 = Video.objects.create(video_type=1, video_id="Q7X3fyId2U0",
            video_account=self.video_account, owner=user)

    def test_video_creation(self):
        """Creates a video object"""
        self.assertEqual(self.pk1.video_id, "Q7X3fyId2U0")
        self.assertEqual(self.pk1.video_link, "https://www.youtube.com/watch?v=Q7X3fyId2U0")
like image 894
thornomad Avatar asked Oct 08 '14 20:10

thornomad


1 Answers

So I finally figured it out and I appreciate everyone's input which got me thinking in the right way:

One of my past migrations had datetime.date.today() as the default value (which is a hint the migrations give). I didn't think about it because at the time I didn't even have any data in the model and then, even though that migration had then been migrated again (further down the road), it appears the test system is running every migration each time it starts. So: was getting that warning.

Update: this should be fixed in 1.7.1.

like image 79
thornomad Avatar answered Sep 20 '22 20:09

thornomad