我正在通过官方文档教程学习django版本1.11.4。我使用python 3.6.5和mysql8作为数据库。我还使用mysql.connector.django连接到mysql数据库。我试着做第一部Django应用程序,第2部分。
This is the link of the example I used
一切正常,除非我运行此命令:
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,
},
}
}
任何线索或帮助修复此错误将不胜感激。
答案 0 :(得分: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:警告:(3719,&#34;&#39; utf8&#39;目前是别名字符集UTF8MB3,在将来的版本中将被UTF8MB4取代。请考虑使用UTF8MB4以便明确。&#34;
我无法弄清楚,但因为它只是一个警告,Django教程似乎工作正常。
请告诉我这是否有帮助,或者如果您有其他问题,我会尽力帮助您。
答案 1 :(得分:1)
我在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$
现在为我工作的 SOLUTION 是
1)再次使用brew安装mysql
brew install mysql
2)使用brew升级mysql到最新版本(如果需要)
brew upgrade mysql
3)现在用pip3安装mysqlclient(在没有virtualenv的情况下全局安装)
pip3 install mysqlclient
4)现在访问virtualenv并在其中安装mysqlclient,它将安装正常,没有任何错误的configparser
答案 2 :(得分:1)
从bug report发布的by @Alasdair可以看出,解决方案是:
在
use_pure=True
中设置DATABASES['default']['OPTIONS']
。
答案 3 :(得分:0)
这是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