【发布时间】:2014-12-03 14:48:21
【问题描述】:
我正在绕圈子,需要一些帮助。我继续收到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)
这是模型代码(有所删节):
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
以及我的设置文件的相关(我认为)部分:
TIME_ZONE = 'UTC'
USE_TZ = True
这是一个 sqlite 问题吗(仍在测试中)?还是我在这里遗漏了一些基本的东西?我已经阅读了here 和here,当然还有文档here。但我很难过。谢谢。
编辑:添加了引发错误的测试
我在运行测试时遇到了错误……我把编辑过的东西留在了那里,但你应该明白了:
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")
【问题讨论】:
-
收到此错误时
-
@HasanRamezani 当我在我的测试套件中创建一个新模型时 - 对不起,我想我应该包括那个......我可以在底部添加它! ...而且,当我使用管理员添加时,我在开发服务器中看不到该警告...仅在运行测试时。
-
删除
USE_TZ = True,这可能会解决您的问题。 -
@HasanRamezani - 它确实消除了警告,但我想要时区支持!所以我的问题还没有解决。微笑。
-
您发布的内容看起来不错,所以问题一定出在其他地方。既然您说它只发生在测试中,请尝试使用
TransactionTestCase而不是TestCase。没关系,但 TTC 通常更安全。发布您未编辑的模型代码,以及您正在运行的确切测试命令。您有多个设置文件吗?
标签: python django sqlite datetime timezone