我正在通过官方文档的教程学习django版本1.11.4。我使用Python3.6.5和mysql8作为数据库。我也使用mysql.connector.django连接到mysql数据库。我试着做第一个Django应用程序,第2部分。
This is the link of the example I used
除了我运行this命令之外,一切都很正常:
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'models文件中使用的代码:
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,
},
}
}任何修复此错误的线索或帮助都将不胜感激。
发布于 2018-05-27 03:00:13
我费了几个小时才用python3在MacOS上使用MySQL DB建立了我的Django项目。我无法通过pip3在使用virtualenv创建的虚拟环境中安装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 mysql2)将带有brew的mysql升级到最新版本(如果需要)
brew upgrade mysql3)现在安装带有pip3的mysqlclient (全局安装,不带virtualenv)
pip3 install mysqlclient4)现在访问virtualenv并在其中安装mysqlclient,它将安装得很好,configparser没有任何错误
发布于 2018-06-07 21:03:02
在bug report发布的by @Alasdair中可以看到,解决方案是:
在
DATABASES['default']['OPTIONS']中设置use_pure=True。
发布于 2018-04-26 05:06:15
我昨天或两天前也经历过这种痛苦,并能够使用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, }, } }
注意引擎是不同的。如果我将引擎更改为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教程似乎工作得很好。
让我知道这是否有帮助,或者如果你有其他问题,我会尽我所能帮助你。
https://stackoverflow.com/questions/49977768
复制相似问题