Opened 7 years ago

Closed 7 years ago

#28827 closed Bug (fixed)

AttributeError: 'int' object has no attribute 'tzinfo' with ExtractHour and calculation

Reported by: Martijnth Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Martijnth)

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 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'

Change History (3)

comment:1 by Martijnth, 7 years ago

Description: modified (diff)

comment:2 by Martijnth, 7 years ago

Description: modified (diff)
Summary: query with ExtractHour and calculation like "values(minutes=(ExtractHour('dt_start') * 60))" gives an pytz error: AttributeError: 'int' object has no attribute 'tzinfo'AttributeError: 'int' object has no attribute 'tzinfo' with ExtractHour and calculation

comment:3 by Tim Graham, 7 years ago

Resolution: fixed
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top