Bug #91197 'datetime.datetime' object has no attribute 'split' with Django
Submitted: 9 Jun 2018 10:28 Modified: 11 Jun 2018 10:52
Reporter: yp ma Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:8.0.11 OS:Any
Assigned to: CPU Architecture:Any

[9 Jun 2018 10:28] yp ma
Description:
AttributeError at /admin/login/

'datetime.datetime' object has no attribute 'split'

Request Method: 	POST
Request URL: 	http://127.0.0.1:8000/admin/login/?next=/admin/
Django Version: 	2.0.6
Exception Type: 	AttributeError
Exception Value: 	

'datetime.datetime' object has no attribute 'split'

Exception Location: 	F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\conversion.py in _DATETIME_to_python, line 506
Python Executable: 	F:\helloword-master\helloword\venv\Scripts\python.exe
Python Version: 	3.6.5
Python Path: 	

['F:\\helloword\\helloword',
 'F:\\helloword',
 'F:\\helloword-master\\helloword\\venv\\Scripts\\python36.zip',
 'C:\\Users\\d\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
 'C:\\Users\\d\\AppData\\Local\\Programs\\Python\\Python36\\lib',
 'C:\\Users\\d\\AppData\\Local\\Programs\\Python\\Python36',
 'F:\\helloword-master\\helloword\\venv',
 'F:\\helloword-master\\helloword\\venv\\lib\\site-packages',
 'F:\\helloword-master\\helloword\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.6.egg',
 'F:\\helloword-master\\helloword\\venv\\lib\\site-packages\\pip-10.0.1-py3.6.egg',
 'D:\\Program Files\\JetBrains\\PyCharm '
 '2018.1.4\\helpers\\pycharm_matplotlib_backend',
 'F:\\helloword',
 'F:\\helloword']

Server time: 	Sat, 9 Jun 2018 09:24:20 +0000

I use MySQL 8.0.11 and mysql-connector-python-8.0.11.
Tested in Mac and windows.
It's a helloworld project.
When I url into django admin, it comes the error.

How to repeat:
Environment:
Windows 10

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/login/?next=/admin/

Django Version: 2.0.6
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'helloWeb']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "F:\helloword-master\helloword\venv\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\admin\sites.py" in login
  398.         return LoginView.as_view(**defaults)(request)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in _wrapper
  62.             return bound_func(*args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\decorators\debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in bound_func
  58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in _wrapper
  62.             return bound_func(*args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in bound_func
  58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in _wrapper
  62.             return bound_func(*args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\utils\decorators.py" in bound_func
  58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\views.py" in dispatch
  66.         return super().dispatch(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\views\generic\edit.py" in post
  141.         if form.is_valid():

File "F:\helloword-master\helloword\venv\lib\site-packages\django\forms\forms.py" in is_valid
  179.         return self.is_bound and not self.errors

File "F:\helloword-master\helloword\venv\lib\site-packages\django\forms\forms.py" in errors
  174.             self.full_clean()

File "F:\helloword-master\helloword\venv\lib\site-packages\django\forms\forms.py" in full_clean
  377.         self._clean_form()

File "F:\helloword-master\helloword\venv\lib\site-packages\django\forms\forms.py" in _clean_form
  404.             cleaned_data = self.clean()

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\forms.py" in clean
  195.             self.user_cache = authenticate(self.request, username=username, password=password)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\__init__.py" in authenticate
  70.             user = _authenticate_with_backend(backend, backend_path, request, credentials)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\__init__.py" in _authenticate_with_backend
  116.     return backend.authenticate(*args, **credentials)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\backends.py" in authenticate
  16.             user = UserModel._default_manager.get_by_natural_key(username)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\contrib\auth\base_user.py" in get_by_natural_key
  44.         return self.get(**{self.model.USERNAME_FIELD: username})

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\query.py" in get
  397.         num = len(clone)

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\query.py" in __len__
  254.         self._fetch_all()

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\query.py" in _fetch_all
  1179.             self._result_cache = list(self._iterable_class(self))

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\query.py" in __iter__
  62.         for row in compiler.results_iter(results):

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\sql\compiler.py" in apply_converters
  1007.         for row in map(list, rows):

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\sql\compiler.py" in cursor_iter
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\models\sql\compiler.py" in <lambda>
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "F:\helloword-master\helloword\venv\lib\site-packages\django\db\utils.py" in inner
  96.                 return func(*args, **kwargs)

File "F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\cursor_cext.py" in fetchmany
  510.             rows.extend(self._cnx.get_rows(size))

File "F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\connection_cext.py" in get_rows
  275.                                                           row[i])

File "F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\conversion.py" in to_python
  205.             return self._cache_field_types[vtype[1]](value, vtype)

File "F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\django\base.py" in _DATETIME_to_python
  119.         dt = MySQLConverter._DATETIME_to_python(self, value)

File "F:\helloword-master\helloword\venv\lib\site-packages\mysql\connector\conversion.py" in _DATETIME_to_python
  506.             (date_, time_) = value.split(b' ')

datetime_val 	

None

dsc 	

None

self 	

<mysql.connector.django.base.DjangoMySQLConverter object at 0x000001BE95C2DC50>

value 	

datetime.datetime(2018, 6, 9, 9, 21, 28, 899553)

Exception Type: AttributeError at /admin/login/
Exception Value: 'datetime.datetime' object has no attribute 'split'

Suggested fix:
the value is already datetime, not str.
Use as datetime or trans into str.
[9 Jun 2018 11:11] yp ma
def _DATETIME_to_python(self, value, dsc=None):  # pylint: disable=C0103
        """
        Returns DATETIME column type as datetime.datetime type.
        """
        datetime_val = None
        if isinstance(value, datetime.datetime):
            datetime_val = value
            return datetime_val
        try:
            (date_, time_) = value.split(b' ')
            if len(time_) > 8:
                (hms, mcs) = time_.split(b'.')
                mcs = int(mcs.ljust(6, b'0'))
            else:
                hms = time_
                mcs = 0
            dtval = [int(i) for i in date_.split(b'-')] + \
                    [int(i) for i in hms.split(b':')] + [mcs, ]
            datetime_val = datetime.datetime(*dtval)
        except ValueError:
            datetime_val = None

        return datetime_val
[11 Jun 2018 10:52] Chiranjeevi Battula
Hello yp ma,

Thank you for the bug report.
This is most likely duplicate of Bug #90541, please see Bug #90541.
Workaround to avoid this is by setting `use_pure=True` in DATABASES['default']['OPTIONS'].

Thanks,
Chiranjeevi.