4

我有一个在 Django 1.6 中构建的博客系统,我正在尝试从我的 Post 模型的 DateTimeField pub_date 呈现一个 YearArchiveView,或者至少获取一个包含帖子的年份列表。它一直告诉我我的 pub_date 是幼稚的,但我已经明确地将它们更改为不幼稚。

这是我在 python shell 上所做的一些操作:

    >>> for post in posts:
...  post.pub_date
... 
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
datetime.datetime(2014, 1, 14, 3, 23, 2, tzinfo=<UTC>)
>>> years = Post.live.datetimes('pub_date', 'year', order='DESC')
/Users/.../django/db/models/fields/__init__.py:903: RuntimeWarning: DateTimeField Post.pub_date received a naive datetime (2014-01-13 21:40:01.051109) while time zone support is active.
  RuntimeWarning)

>>> years
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/.../django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/.../db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/Users/.../django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/.../django/db/models/sql/compiler.py", line 1107, in results_iter
    raise ValueError("Database returned an invalid value "
ValueError: Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed?
>>> import pytz
>>> posts[0].posted
datetime.datetime(2013, 9, 26, 0, 48, 8, tzinfo=<UTC>)
>>> 

这到底是怎么回事?我要疯了!

4

3 回答 3

1

Check that USE_TZ is true. See the docs on Django 1.6 changes to queryset.dates

于 2014-01-14T04:32:13.730 回答
1

我刚刚在模型上创建了一个单独的 DateField,然后将 DateTimeField 的日期保存到它,所以我不必为新的 .datetimes 查询集而烦恼。

这似乎不是最好的解决方案,但我不需要每月存档的时间,所以它现在有效。

于 2014-01-15T02:01:53.943 回答
1

您是否使用 SQLite 作为数据库?它不支持datetimes中的时区。

于 2014-01-14T06:07:02.810 回答