Django: DateTimeQuery object has no attribute tzinfo when USE_TZ is True

1.9k views Asked by At

I am using postgres as my database. I have a model called Invoice with a datetime field. I get an error when I run the query:

Invoice.objects.datetimes('date_field','day')

The error is:

AttributeError: 'DateTimeQuery' object has no attribute 'tzinfo'

This happens when USE_TZ setting is set to True in my settings file.

The datetimes query needs a tzinfo object and if it is none the default tzinfo object which is utc is used. I tried importing the utc tzinfo object from django.utils.timezone and used it in the query like this:

from django.utils.timezone import utc
Invoice.objects.datetimes('date_field','day', tzinfo=utc)

Even then I am getting the same error.

I have read the documentation on django docs about datetimes query and it says that when using postgres pytz is not required. But I installed pytz anyway and tried again but still got the error.

When I set USE_TZ to false the query runs fine and there are no errors.

I found this ticket on django website which reports this error but I do not know how to use the patch it talks about.

https://code.djangoproject.com/ticket/21432

Can someone please tell me how to run the query without errors with USE_TZ=True.

1

There are 1 answers

0
zaphod100.10 On BEST ANSWER

Upgrading to version 1.6.1 or later solves the problem. Got the answer from django users google group.