Time zone support enabled in Django 1.4, yet "Django does not support timezone-aware times"

104 views
Skip to first unread message

Arthur

unread,
May 21, 2012, 8:57:05 PM5/21/12
to django...@googlegroups.com
Hello,

I recently upgraded one of my machines to Django 1.4 to patch a bug related to the use of PostGIS 9.1. I read the documentation on the new support for time zones and added USE_TZ=True to my settings.py file. Nonetheless, when I attempt to load data into my PostGIS database in one of the following ways, I receive a ValueError: Django does not support timezone-aware times. The full traceback is posted at the end.

In the clean() method of my model:
    self.datetime = datetime.datetime.combine(self.date, self.time).replace(tzinfo=UTC())

Or in the handle() method of a Command instance:
    data_dict['datetime'] = datetime.datetime.combine(data_dict['date'], data_dict['time']).replace(tzinfo=UTC())

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 458, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 381, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 256, in execute
    output = self.handle(*args, **options)
  File "/usr/local/dev/gass/../gass/bering/management/commands/load_station_data.py", line 108, in handle
    data_obj.save() # Save the record to the database only if it doesn't already exist
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 483, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 578, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 203, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1581, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 906, in execute_sql
    for sql, params in self.as_sql():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 869, in as_sql
    for obj in self.query.objs
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 296, in get_db_prep_save
    prepared=False)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1258, in get_db_prep_value
    return connection.ops.value_to_db_time(value)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 809, in value_to_db_time
    raise ValueError("Django does not support timezone-aware times.")
ValueError: Django does not support timezone-aware times.

Any advice is appreciated. Thanks!

Ramiro Morales

unread,
May 22, 2012, 5:19:03 PM5/22/12
to django...@googlegroups.com
On Mon, May 21, 2012 at 9:57 PM, Arthur <kaen...@mtu.edu> wrote:
> Hello,
>
> I recently upgraded one of my machines to Django 1.4 to patch a bug related
> to the use of PostGIS 9.1. I read the documentation on the new support for
> time zones and added USE_TZ=True to my settings.py file. Nonetheless, when I
> attempt to load data into my PostGIS database in one of the following ways,
> I receive a ValueError: Django does not support timezone-aware times.

If what you did in that machine is upgrade to Django 1.4 from an older version
then you might be victim of a problem because of whcih the files form the
previous installation aren't overwritten and you end with a broken mix of
things.

The documentation has recently been [1]amended with instructions on how
to avoid this. Please use the [2]updated section in the relevant document.

HTH

--
Ramiro Morales

1. https://github.com/django/django/commit/6ed7d40727f70934df6ab0ac96f5f1c4f01c534f
2. https://docs.djangoproject.com/en/dev/topics/install/#remove-any-old-versions-of-django

Aymeric Augustin

unread,
Jun 22, 2012, 5:11:18 AM6/22/12
to django...@googlegroups.com
Le mardi 22 mai 2012 02:57:05 UTC+2, Arthur a écrit :
I recently upgraded one of my machines to Django 1.4 to patch a bug related to the use of PostGIS 9.1. I read the documentation on the new support for time zones and added USE_TZ=True to my settings.py file. Nonetheless, when I attempt to load data into my PostGIS database in one of the following ways, I receive a ValueError: Django does not support timezone-aware times. The full traceback is posted at the end. 

Hi Arthur,

The error message is about "timezone-aware times", not "timezone-aware datetimes". Your code must be creating a timezone-aware time object somewhere. This is quite a pathological type and it isn't supported by Django.

This limitation is briefly mentioned at the bottom of this section: https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects

Sorry for the late answer,

-- 
Aymeric.
Reply all
Reply to author
Forward
0 new messages