[Django] #28827: query with ExtractHour and calculation like "values(minutes=(ExtractHour('dt_start') * 60))" gives an pytz error: AttributeError: 'int' object has no attribute 'tzinfo'

15 views
Skip to first unread message

Django

unread,
Nov 21, 2017, 11:34:32 PM11/21/17
to django-...@googlegroups.com
#28827: query with ExtractHour and calculation like
"values(minutes=(ExtractHour('dt_start') * 60))" gives an pytz error:
AttributeError: 'int' object has no attribute 'tzinfo'
-------------------------------------+-------------------------------------
Reporter: Martijnth | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The generated query works fine in MySQL, guess Django still thinks the
result is a datetime/timezone object
but it has become an integer because of the calculation.

---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)
<ipython-input-1-b1fc61315659> in <module>()
---> 47 print(a)

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/query.py in __repr__(self)
224
225 def __repr__(self):
--> 226 data = list(self[:REPR_OUTPUT_SIZE + 1])
227 if len(data) > REPR_OUTPUT_SIZE:
228 data[-1] = "...(remaining elements truncated)..."

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/query.py in __iter__(self)
248 - Responsible for turning the rows into model
objects.
249 """
--> 250 self._fetch_all()
251 return iter(self._result_cache)
252

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/query.py in _fetch_all(self)
1116 def _fetch_all(self):
1117 if self._result_cache is None:
-> 1118 self._result_cache = list(self._iterable_class(self))
1119 if self._prefetch_related_lookups and not
self._prefetch_done:
1120 self._prefetch_related_objects()

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/query.py in __iter__(self)
104 names = extra_names + field_names + annotation_names
105
--> 106 for row in compiler.results_iter():
107 yield dict(zip(names, row))
108

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/sql/compiler.py in results_iter(self,
results)
840 for row in rows:
841 if converters:
--> 842 row = self.apply_converters(row, converters)
843 yield row
844

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/models/sql/compiler.py in apply_converters(self,
row, converters)
825 value = row[pos]
826 for converter in convs:
--> 827 value = converter(value, expression,
self.connection, self.query.context)
828 row[pos] = value
829 return tuple(row)

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/db/backends/mysql/operations.py in
convert_datetimefield_value(self, value, expression, connection, context)
237 if value is not None:
238 if settings.USE_TZ:
--> 239 value = timezone.make_aware(value,
self.connection.timezone)
240 return value
241

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/django/utils/timezone.py in make_aware(value, timezone,
is_dst)
283 if hasattr(timezone, 'localize'):
284 # This method is available for pytz time zones.
--> 285 return timezone.localize(value, is_dst=is_dst)
286 else:
287 # Check that we won't overwrite the timezone of an aware
datetime.

/Users/martijntenhoor/projects/bluebi/rms-drf-api/virtual/lib/python3.5
/site-packages/pytz/__init__.py in localize(self, dt, is_dst)
225 '''Convert naive time to local time'''
226 print(dt)
--> 227 if dt.tzinfo is not None:
228 raise ValueError('Not naive datetime (tzinfo is
already set)')
229 return dt.replace(tzinfo=self)

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

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

Django

unread,
Nov 21, 2017, 11:35:17 PM11/21/17
to django-...@googlegroups.com
#28827: query with ExtractHour and calculation like
"values(minutes=(ExtractHour('dt_start') * 60))" gives an pytz error:
AttributeError: 'int' object has no attribute 'tzinfo'
-------------------------------------+-------------------------------------
Reporter: Martijnth | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Martijnth:

Old description:

New description:

}}}

--

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

Django

unread,
Nov 21, 2017, 11:37:08 PM11/21/17
to django-...@googlegroups.com
#28827: AttributeError: 'int' object has no attribute 'tzinfo' with ExtractHour and
calculation

-------------------------------------+-------------------------------------
Reporter: Martijnth | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Martijnth:

Old description:

> The generated query works fine in MySQL, guess Django still thinks the

New description:

Query with ExtractHour and calculation like


"values(minutes=(ExtractHour('dt_start') * 60))" gives an pytz error:
AttributeError: 'int' object has no attribute 'tzinfo'

The generated query works fine in MySQL, guess Django still thinks the

}}}

--

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

Django

unread,
Nov 22, 2017, 6:38:09 PM11/22/17
to django-...@googlegroups.com
#28827: AttributeError: 'int' object has no attribute 'tzinfo' with ExtractHour and
calculation

-------------------------------------+-------------------------------------
Reporter: Martijnth | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

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


Comment:

I guess you meant to use `QuerySet.annotate()` instead of `values()` in
the description since the latter doesn't accept a `minutes` keyword
argument.

Based on my testing of that, it seems this is fixed in Django 2.0 by
504ce3914fa86a58f29f5369a806f3fe56a0d59a. If not, please reopen with more
explicit steps (model/queryset) to reproduce.

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

Reply all
Reply to author
Forward
0 new messages