【问题标题】:error ('datetime.datetime' object has no attribute 'split') in django 1.11.4django 1.11.4 中的错误('datetime.datetime' 对象没有属性'split')
【发布时间】:2018-10-03 07:17:05
【问题描述】:

我正在通过官方文档上的教程学习 django 版本 1.11.4。我正在使用 python 3.6.5 和 mysql8 作为数据库。我还使用 mysql.connector.django 连接到 mysql 数据库。我尝试做第一个 Django 应用,第 2 部分。

This is the link of the example I used

一切正常,除非我运行此命令:

问题.objects.all()

我收到以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
    for row in compiler.results_iter(results):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 839, in results_iter
    for rows in results:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1284, in cursor_iter
    sentinel):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py", line 101, in inner
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 510, in fetchmany
    rows.extend(self._cnx.get_rows(size))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 275, in get_rows
    row[i])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 205, in to_python
    return self._cache_field_types[vtype[1]](value, vtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/django/base.py", line 119, in _DATETIME_to_python
    dt = MySQLConverter._DATETIME_to_python(self, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 506, in _DATETIME_to_python
    (date_, time_) = value.split(b' ')
AttributeError: 'datetime.datetime' object has no attribute 'split'

模型文件中使用的代码:

import datetime

from django.db import models

from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - 
        datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text



class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
    return self.choice_text

数据库设置:

DATABASES = {
    'default': {
        'NAME': 'mysite',
        'ENGINE': 'mysql.connector.django',
        'USER': 'root',
        'PASSWORD': '********',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

任何解决此错误的线索或帮助将不胜感激。

【问题讨论】:

  • 错误似乎类似于this bug report。 Django 文档警告MySQL connector may not support the most recent releases of Django。您可以尝试使用推荐的包mysqlclient
  • 我花了大约 2 天时间在 mac 上安装 mysqlclient,但没有成功。这就是我切换到mysql连接器的原因。我尝试了所有可能的解决方案来修复不同的错误,但没有运气。
  • 这是我尝试安装 mysqlclient 时无法修复的错误
  • 命令 "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/cg/ ttg6lfr17pb005f63_0y7b6r0000gn/T/pip-install-qwt4df54/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace( '\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /private/var/folders /cg/ttg6lfr17pb005f63_0y7b6r0000gn/T/pip-record-apcxyrd9/install-record.txt --single-version-externally-managed --compile"
  • 在 /private/var/folders/cg/ttg6lfr17pb005f63_0y7b6r0000gn/T/pip-install-qwt4df54/mysqlclient/ 中出现错误代码 1 失败

标签: mysql django python-3.x


【解决方案1】:

我昨天或 2 天前也经历过这种痛苦,并且能够使用 mysqlclient-1.3.12 运行它。我要从记忆中走出来,所以请耐心等待,我尝试了很多东西,但最终我让它发挥了作用。

我像你一样从 mysql 网站安装了 mysql8 和 mysql8connector,但没有得到爱。经过大量搜索和反复试验,我在某个地方找到了答案,但我最终还是做了:

brew install mysql pip install mysqlclient

我知道您已经安装了 mysql,但 brew install mysql 似乎添加了用于编译 mysqlclient 连接器的客户端库。然后我的设置文件中的数据库如下所示:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'polls', 'USER': 'myuser', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'charset': 'utf8mb4', 'autocommit': True, }, } }

注意 ENGINE 是不同的。如果我将 ENGINE 更改为 mysql.connector.django,我可以复制您的确切错误。

但是,使用 django.db.backends.mysql 我仍然收到以下警告:

lib/python3.6/site-packages/django/db/backends/mysql/base.py:71: Warning: (3719, "'utf8' 当前是字符集 UTF8MB3 的别名,将被替换在未来的版本中由 UTF8MB4 提供。请考虑使用 UTF8MB4 以明确。”

我不知道,但由于它只是一个警告,Django 教程似乎工作正常。

如果这有帮助,或者如果您有其他问题,请告诉我,我会尽力提供帮助。

【讨论】:

    【解决方案2】:

    我花了几个小时在 MacOS 上使用 MySQL DB 使用 python3 设置我的 Django 项目。 我无法在使用 virtualenv 创建的虚拟环境中通过 pip3 安装 mysqlclient 和 MySQL-Python

    错误堆栈跟踪是:something wrong due to configparser in python3

    Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python
    Downloading/unpacking MySQL-python
    Running setup.py egg_info for package MySQL-python
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
        from setup_posix import get_config
      File "./setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ImportError: No module named 'ConfigParser'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
    
    File "<string>", line 16, in <module>
    
    File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
    
    from setup_posix import get_config
    
    File "./setup_posix.py", line 2, in <module>
    
    from ConfigParser import SafeConfigParser
    
    ImportError: No module named 'ConfigParser'
    
    ----------------------------------------
    Command python setup.py egg_info failed with error code 1 in /var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python
    Storing complete log in /Users/jan/.pip/pip.log
    Jans-MacBook-Pro:~ jan$ 
    

    现在对我有用的解决方案

    1) 再次用brew安装mysql

    brew install mysql
    

    2) 使用 brew 将 mysql 升级到最新版本(如果需要)

    brew upgrade mysql
    

    3) 现在用 pip3 安装 mysqlclient(全局安装,不用 virtualenv)

    pip3 install mysqlclient
    

    4) 现在访问 virtualenv 并在其中安装 mysqlclient,它将正常安装而 configparser 没有任何错误

    【讨论】:

      【解决方案3】:

      bug reportby @Alasdair可以看出,解决方法是:

      DATABASES['default']['OPTIONS'] 中设置use_pure=True

      【讨论】:

      • 谢谢@adelin,这对我来说很有帮助。对于可能需要此功能的其他人,我在使用 Django 1.11.14、mysql-connector-python 8.0.11 和 mysql 5.7 获取具有 DateTimeField 的模型时遇到了问题。这解决了问题。
      【解决方案4】:

      这是 mysql-connector-python 数据转换问题。您可以通过使用 use_pure=true 设置数据库选项参数来解决此问题。当前版本 8.0.11、8.0.12 和 8.0.13 中未修复此问题。

      "OPTIONS":{
       "use_pure":True
      }
      

      这只是一个临时解决方案。更多信息请参考这里:https://bugs.mysql.com/bug.php?id=90541

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 2022-11-29
        • 1970-01-01
        • 2016-07-25
        • 2013-08-23
        • 2015-05-17
        相关资源
        最近更新 更多