Why is Django calling my datetimes naive when they are not?

528 views Asked by At

I've got a blog system I'm building in Django 1.6, and I'm trying to render a YearArchiveView, or at least get a list of years with posts, from my Post model's DateTimeField, pub_date. It keeps telling me my pub_date is naive, but I've explicitly changed them not to be.

Here's some puttering I've done on the 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>)
>>> 

What the hell is going on? I'm going crazy!

3

There are 3 answers

0
sambacore On BEST ANSWER

I just ended up making a separate DateField on the model, then saved the DateTimeField's date to it, so I don't have to bother with the new .datetimes queryset.

It doesn't seem like the best solution, but I don't need times for monthly archives, so it works for now.

1
Kevin Stone On

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

1
tuomur On

Are you using SQLite as a database? It has not support for timezones in datetimes.