Django: Naive datetime while time zone support is active (sqlite)
我在这件事上绕圈子,需要帮助。我继续得到一个
以下是警告:
1 2 | /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) |
下面是模型代码(稍微修改了一下):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 |
号
以及设置文件的相关部分:
1 2 | TIME_ZONE = 'UTC' USE_TZ = True |
这是否是一个sqlite问题(仍在测试东西)?或者我在这里遗漏了一些基本的东西?我在这里和这里读过,当然在这里的医生那里也读过。但我被难住了。谢谢。
编辑:添加了引发错误的测试运行测试时出错…我把修改过的东西留在里面了,但你应该明白:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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") |
。
所以我终于明白了这一点,我很感激每个人的意见,这让我以正确的方式思考:
我过去的一次迁移将
更新:应在1.7.1中修复。
您正在使用sqlite数据库,而sqlite db不支持时区。这导致了警告。
可以使用其他数据库后端删除此警告。
如果您想使用sqlite,可能将这些行放入设置文件可以帮助:
1 2 3 | import warnings import exceptions warnings.filterwarnings("ignore", category=exceptions.RuntimeWarning, module='django.db.backends.sqlite3.base', lineno=53) |
您是否安装了http://pytz.sourceforge.net/?
As soon as you activate time zone support, Django needs a definition of the default time zone. When pytz is available, Django loads this definition from the tz database. This is the most accurate solution. Otherwise, it relies on the difference between local time and UTC, as reported by the operating system, to compute conversions. This is less reliable, especially around DST transitions.
号