tho*_*mad 11 python sqlite django timezone datetime
我正在围绕这个问题,需要一些帮助.我继续得到naive timezone警告.不确定我做错了什么!精氨酸.
这是警告:
/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)
Run Code Online (Sandbox Code Playgroud)
这是模型代码(稍微编辑):
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
Run Code Online (Sandbox Code Playgroud)
以及我的设置文件的相关(我认为)部分:
TIME_ZONE = 'UTC'
USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
这是一个sqlite问题(我还在测试东西)吗?或者我错过了一些基本的东西?我已经在这里和这里阅读了它,当然还有在这里的文档.但我很难过.谢谢.
我在运行测试时遇到错误...我将编辑后的内容留在那里,但你应该明白这个想法:
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='jacob@test.com', 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")
Run Code Online (Sandbox Code Playgroud)
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.