Opened 3 years ago

Last modified 7 weeks ago

#30224 assigned Bug

Mysql Datetime value is string instead of datetime object — at Initial Version

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


The Problem
Datetimes are sometimes returned as objects or strings. It depends on the value. E.g. '2019-03-01 00:00:00.000000' is returned as a string, while2018-12-12 22:02:53.134000 is provided as a datetime object in the sql/

My Table


id char(32) COLLATE utf8mb4_unicode_ci NOT NULL,
created_on datetime(6) NOT NULL,
modified_on datetime(6) NOT NULL,
first_spot_broadcast datetime(6) DEFAULT NULL,
expiry_date datetime(6) DEFAULT NULL


DB driver: django.db.backends.mysql
Django version: 2.0.4 tried 2.2 as well
DRF 3.8.2
mysqlclient = "==1.4.2"

This issue only occurred for me when I execute this query.

        minExpiryDate=Min('buyout__expiry_date', filter=Q(buyout__is_archived=False, buyout__is_deleted=False))) \
        .filter(Q(minExpiryDate__gte=(now + expiryInterval))
                & Q(minExpiryDate__lte=(now + expiryInterval + timedelta(days=1))))

I tried upgrading to 2.2 and the issue persisted.

My stack trace is:

Traceback (most recent call last):
  File "/usr/share/pycharm/helpers/pydev/", line 1741, in <module>
  File "/usr/share/pycharm/helpers/pydev/", line 1735, in main
    globals =['file'], None, None, is_module)
  File "/usr/share/pycharm/helpers/pydev/", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/usr/share/pycharm/helpers/pydev/_pydev_imps/", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/server/", line 15, in <module>
  File "site-packages/django/core/management/", line 381, in execute_from_command_line
  File "site-packages/django/core/management/", line 375, in execute
  File "site-packages/django/core/management/", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "site-packages/django/core/management/", line 353, in execute
    output = self.handle(*args, **options)
  File "/server/buyout/management/commands/", line 8, in handle
  File "site-packages/celery/", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "site-packages/celery/app/", line 375, in __call__
    return*args, **kwargs)
  File "/server/buyout/tasks/", line 46, in send_expiry_reminder_summary
    if len(projects_expiring) > 0:
  File "site-packages/django/db/models/", line 250, in __len__
  File "site-packages/django/db/models/", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "site-packages/django/db/models/", line 63, in __iter__
    for row in compiler.results_iter(results):
  File "site-packages/django/db/models/sql/", line 1009, in apply_converters
    value = converter(value, expression, connection)
  File "site-packages/django/db/backends/mysql/", line 252, in convert_datetimefield_value
    value = timezone.make_aware(value, self.connection.timezone)
  File "site-packages/django/utils/", line 264, in make_aware
    return timezone.localize(value, is_dst=is_dst)
  File "site-packages/pytz/", line 222, in localize
    if dt.tzinfo is not None:
AttributeError: 'str' object has no attribute 'tzinfo'
Exception ignored in: <generator object cursor_iter at 0x7f7ab3f88390>
Traceback (most recent call last):
  File "site-packages/django/db/models/sql/", line 1469, in cursor_iter
  File "site-packages/MySQLdb/", line 86, in close
    while self.nextset():
  File "site-packages/MySQLdb/", line 136, in nextset
    nr = db.next_result()
MySQLdb._exceptions.OperationalError: (2006, '')

Change History (0)

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