[Django] #21432: datetimes method always raise AttributeError

11 views
Skip to first unread message

Django

unread,
Nov 13, 2013, 6:12:00 AM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
----------------------------------------------+--------------------
Reporter: Enrique Martínez <enrique@…> | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
NewsItem.objects.all().datetimes('date_time', 'month', order='DESC')
always raises: AttributeError: 'DateTimeQuery' object has no attribute
'tzinfo'

and it happens with all my models with some DateTimeField.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 13, 2013, 12:23:13 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* severity: Normal => Release blocker
* cc: timo (added)
* needs_better_patch: => 0
* component: Uncategorized => Core (Other)
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

I can reproduce this on the tutorial with the following query:
`Poll.objects.datetimes('pub_date', 'month')` (Python 2.7, pytz 2013.8,
settings.USE_TZ=True). Tentatively marking as a release blocker as I don't
see anything wrong with that based on the documentation. On the other
hand, we have tests that seem to test the same thing, so wihout digging in
I'm unsure what the issue might be.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:1>

Django

unread,
Nov 13, 2013, 2:44:23 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by timo):

{{{


>>> Poll.objects.datetimes('pub_date', 'month')

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/timgraham/code/django/django/db/models/query.py", line 115,
in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/timgraham/code/django/django/db/models/query.py", line 140,
in __iter__
self._fetch_all()
File "/home/timgraham/code/django/django/db/models/query.py", line 962,
in _fetch_all
self._result_cache = list(self.iterator())
File "/home/timgraham/code/django/django/db/models/sql/compiler.py",
line 1089, in results_iter
datetime = timezone.make_aware(datetime, self.query.tzinfo)


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

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:2>

Django

unread,
Nov 13, 2013, 3:09:18 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by bmispelon):

Bisecting the error lead me to commit
70679243d1786e03557c28929f9762a119e3ac14, but it's not really obvious (to
me anyway) why that would have broken the feature.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:3>

Django

unread,
Nov 13, 2013, 4:17:28 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by akaariai):

It is possible the above commit just makes the error loud instead of
hiding the error and returning silently []. Before the commit errors
inside queryset iteration led to empty list as result.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:4>

Django

unread,
Nov 13, 2013, 5:44:28 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by bmispelon):

* cc: bmispelon (added)


Comment:

Replying to [comment:4 akaariai]:


> It is possible the above commit just makes the error loud instead of
hiding the error and returning silently []. Before the commit errors
inside queryset iteration led to empty list as result.

Indeed, this appears to be the case.

While digging, I also found out that the following code works:
{{{#!python
qs = Poll.objects.datetimes('pub_date', 'month')
list(qs)
repr(qs)
}}}

While this one doesn't (note how `repr` and `list` have been switched):
{{{#!python
qs = Poll.objects.datetimes('pub_date', 'month')
repr(qs)
list(qs)
}}}

This explains why this issue has been able to fall through the cracks:
iterating over a queryset is a much more common use-case than displaying
it as-is.

Unfortunately, I haven't been able to identify exactly where the problem
lies (I have a suspicion that it's got something to do with the `_clone`
method though) but I've managed to come up with a failing testcase (see
attached).

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:5>

Django

unread,
Nov 13, 2013, 6:23:01 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by akaariai):

Yeah, the problem seem to be that when doing qs.datetimes() the
DateTimeQuery gets a tzinfo attribute (datetimes() end up in here:
https://github.com/django/django/blob/master/django/db/models/query.py#L1255),
but if the query gets cloned afterwards the tzinfo attribute will not get
cloned. The solution seems to be to write a custom .clone() for
DateTimeQuery.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:6>

Django

unread,
Nov 13, 2013, 11:57:22 PM11/13/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: nobody
<enrique@…> | Status: new
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by loic84):

* cc: loic@… (added)
* has_patch: 0 => 1


Comment:

PR https://github.com/django/django/pull/1919.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:7>

Django

unread,
Nov 14, 2013, 4:18:14 AM11/14/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: assigned

Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => assigned
* owner: nobody => aaugustin


Comment:

The patch looks appropriate.

The test could be simplified a bit with `now =
timezone.now().replace(microsecond=0)`.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:8>

Django

unread,
Nov 14, 2013, 5:15:41 AM11/14/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: assigned
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution:
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

* stage: Accepted => Ready for checkin


Comment:

Patch looks good.

It fixes the issue and the full test suite still passes after applying it.

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:9>

Django

unread,
Nov 14, 2013, 3:38:11 PM11/14/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: closed
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution: fixed

Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Baptiste Mispelon <bmispelon@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"17ed99f3a3eea4bd27fa34be59c3582616ed8079"]:
{{{
#!CommitTicketReference repository=""
revision="17ed99f3a3eea4bd27fa34be59c3582616ed8079"
Fixed #21432 -- DateTimeQuery now copies tzinfo when cloning.

Thanks Enrique Martínez for the report and @bmispelon for the tests.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:10>

Django

unread,
Nov 14, 2013, 3:40:11 PM11/14/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: closed
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution: fixed
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Baptiste Mispelon <bmispelon@…>):

In [changeset:"67c30426c1370f5d6c39bd73888c3902c1c5f365"]:
{{{
#!CommitTicketReference repository=""
revision="67c30426c1370f5d6c39bd73888c3902c1c5f365"
[1.6.x] Fixed #21432 -- DateTimeQuery now copies tzinfo when cloning.

Thanks Enrique Martínez for the report and @bmispelon for the tests.

Backport of 17ed99f3a3eea4bd27fa34be59c3582616ed8079 from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:11>

Django

unread,
Nov 15, 2013, 8:05:27 AM11/15/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: closed
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution: fixed
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Loic Bistuer <loic.bistuer@…>):

In [changeset:"32e75803be8b3e9c35e6735c265125c4130ffabc"]:
{{{
#!CommitTicketReference repository=""
revision="32e75803be8b3e9c35e6735c265125c4130ffabc"
Fixed typo and slightly improved error message when db is missing time
zone definitions.

Refs #21432.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:12>

Django

unread,
Dec 26, 2013, 1:07:26 PM12/26/13
to django-...@googlegroups.com
#21432: datetimes method always raise AttributeError
-------------------------------------+-------------------------------------
Reporter: Enrique Martínez | Owner: aaugustin
<enrique@…> | Status: closed
Type: Bug | Version: 1.6
Component: Core (Other) | Resolution: fixed
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3bb7de8c7c3f9d5de5426d94f45fe83b5d998357"]:
{{{
#!CommitTicketReference repository=""
revision="3bb7de8c7c3f9d5de5426d94f45fe83b5d998357"
[1.6.x] Fixed typo and slightly improved error message when db is missing
time zone definitions.

Refs #21432.

Backport of 32e75803be from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21432#comment:13>

Reply all
Reply to author
Forward
0 new messages