error('datetime.datetime'对象没有属性'split')

cl25kdpy  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(581)

我正在学习django 1.11.4版的官方文档教程。我正在使用python3.6.5和mysql8作为数据库。我还使用mysql.connector.django连接到mysql数据库。我试着做第一个django应用程序,第二部分。
这是我使用的示例的链接
一切正常,除了运行此命令:
question.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,
        },
    }
}

任何线索或帮助修复此错误将不胜感激。

x0fgdtte

x0fgdtte1#

这是mysql连接器python的数据转换问题。您可以通过使用use\u pure=true设置database options参数来解决这个问题。当前版本8.0.11、8.0.12和8.0.13中未修复此问题。

"OPTIONS":{
 "use_pure":True
}

这只是暂时的解决办法。请参阅此处了解更多信息:https://bugs.mysql.com/bug.php?id=90541

ma8fv8wu

ma8fv8wu2#

我花了几个小时在macos上用mysql数据库和python3建立django项目。我无法在用virtualenv创建的虚拟环境中安装mysqlclient和mysql python by pip3
错误stacktrace was:由于python3中的configparser而出错

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来说,它安装得很好,没有任何错误

eqqqjvef

eqqqjvef3#

从@alasdair发布的bug报告中可以看出,解决方案是:
use_pure=TrueDATABASES['default']['OPTIONS'] .

sycxhyv7

sycxhyv74#

我昨天或者两天前也经历过这种痛苦,并且能够用mysqlclient-1.3.12运行它。我是从记忆中走出来的,所以请容忍我,我尝试了很多事情,但最终我成功了。
我和你一样从mysql网站上安装了mysql8和mysql8connector,但是没有得到爱。经过反复的寻找和反复的尝试,我在某个地方找到了一个我找不到的答案,但我最终还是做了: brew install mysql pip install mysqlclient 我知道您已经安装了mysql,但是brew install mysql似乎添加了用于编译mysqlclient connector的客户端库。然后,我的设置文件中的数据库如下所示: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'polls', 'USER': 'myuser', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '3306', 'OPTIONS': { 'charset': 'utf8mb4', 'autocommit': True, }, } } 注意发动机不同。如果我将引擎更改为mysql.connector.django,我可以复制您的确切错误。
但是,使用django.db.backends.mysql我仍然得到以下警告:
lib/python3.6/site packages/django/db/backends/mysql/base.py:71:警告:(3719,“'utf8'当前是字符集utf8mb3的别名,在将来的版本中将被utf8mb4替换。请考虑使用utf8mb4以便明确。”
我不知道,但这只是一个警告,django教程似乎运行良好。
让我知道这是否有帮助,或者如果你有其他问题,我会尽我所能帮助。

相关问题