[英]django+mysql='DatabaseWrapper' object has no attribute 'Database' error
I've just installed Python 3.3.0
, mysql-connector
and Django
.我刚刚安装了Python 3.3.0
、 mysql-connector
和Django
。 Then I created my first application called mysite.然后我创建了我的第一个应用程序 mysite。 In settings.py
I added these lines:在settings.py
我添加了以下几行:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306',
}
}
When I run the server and enter the admin page 127.0.0.1:8000/admin/
, I see a long list of errors starting with AttributeError at /admin/ 'DatabaseWrapper' object has no attribute 'Database'
.当我运行服务器并进入管理页面127.0.0.1:8000/admin/
,我看到一长串以AttributeError at /admin/ 'DatabaseWrapper' object has no attribute 'Database'
开头的错误, AttributeError at /admin/ 'DatabaseWrapper' object has no attribute 'Database'
。 I do not know what to do with all this stuff.我不知道如何处理所有这些东西。 The full error description is:完整的错误描述是:
AttributeError at /admin/
'DatabaseWrapper' object has no attribute 'Database'
Request Method: POST
Request URL: http://localhost:8000/admin/
Django Version: 1.6.1
Exception Type: AttributeError
Exception Value:
'DatabaseWrapper' object has no attribute 'Database'
Exception Location: C:\Python33\lib\site-packages\django\db\utils.py in __exit__, line 86
Python Executable: C:\Python33\python.exe
Python Version: 3.3.0
Python Path:
['C:\\mysite',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']
EDIT编辑
When I run python manage.py syncdb, I also get a long list of errors:当我运行 python manage.py syncdb 时,我也会得到一长串错误:
C:\mysite>python manage.py syncdb
Creating tables ...
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line
399, in execute_from_command_line
utility.execute()
File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line
392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python33\lib\site-packages\django\core\management\base.py", line 242,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python33\lib\site-packages\django\core\management\base.py", line 285,
in execute
output = self.handle(*args, **options)
File "C:\Python33\lib\site-packages\django\core\management\base.py", line 415,
in handle
return self.handle_noargs(**options)
File "C:\Python33\lib\site-packages\django\core\management\commands\syncdb.py"
, line 96, in handle_noargs
sql, references = connection.creation.sql_create_model(model, self.style, se
en_models)
File "C:\Python33\lib\site-packages\django\db\backends\creation.py", line 83,
in sql_create_model
model, f, known_models, style)
TypeError: sql_for_inline_foreign_key_references() takes 4 positional arguments
but 5 were given
Ok, so I was experiencing exactly the same problem.好的,所以我遇到了完全相同的问题。 There are lots of suggestions on how you can modify part of existing libraries to make Python3 work with MySQL, but I didn't find any of them to work 100%.关于如何修改现有库的一部分以使 Python3 与 MySQL 一起工作有很多建议,但我没有发现它们中的任何一个可以 100% 工作。 I wasn't able to make official MySQL Python connector to work with Django and Python 3.3.我无法使官方 MySQL Python 连接器与 Django 和 Python 3.3 一起使用。
What did work was switching to PyMySQL library instead.有效的是切换到 PyMySQL 库。 Few months back I already tried it but it didn't work for me back then.几个月前我已经尝试过了,但当时它对我不起作用。 Now, there is a new version, 0.6.1 which worked out of the box.现在,有一个开箱即用的新版本 0.6.1。 So, few more details:所以,还有更多细节:
My environment: OSX 10.9 , Python 3.3.3, Django 1.6.1, MyPySQL 0.6.1, MySQL Server 5.5 on Windows我的环境:OSX 10.9、Python 3.3.3、Django 1.6.1、MyPySQL 0.6.1、Windows 上的 MySQL Server 5.5
How to make it work:如何使它工作:
Install PyMySQL version 0.6.1 ( https://github.com/PyMySQL/PyMySQL/ ): you can install it either by using pip, ie : pip install PyMySQL
or by manually downloading the package;安装 PyMySQL 0.6.1 版本 ( https://github.com/PyMySQL/PyMySQL/ ):您可以使用pip install PyMySQL
,即: pip install PyMySQL
或手动下载软件包; there is a good documentation on their website on how to do that.他们的网站上有关于如何做到这一点的很好的文档。
Open your Django App __init__.py and paste the following lines:打开你的 Django 应用__init__.py并粘贴以下几行:
import pymysql pymysql.install_as_MySQLdb()
Now, open settings.py and make sure your DATABASE property looks like this:现在,打开settings.py并确保您的 DATABASE 属性如下所示:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydb', 'USER': 'dbuser', 'PASSWORD': 'dbpassword', 'HOST': 'dbhost', 'PORT': '3306' } }
That's it, you should be able to execute python manage.py syncdb
so init your MySQL DB;就是这样,你应该能够执行python manage.py syncdb
来初始化你的 MySQL 数据库; see the sample output below:请参阅下面的示例输出:
Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions ... ... Creating table socialaccount_socialtoken You just installed Django's auth system, which means you don't have any superusers defined. ...
I had same error for Django 1.8.9, Python 3.6.12.我对 Django 1.8.9、Python 3.6.12 有同样的错误。 And I was using django-transaction-hooks==0.2
.我正在使用django-transaction-hooks==0.2
。 And my DATABASES settings were我的 DATABASES 设置是
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
.
.
I fixed the issue by using DATABASES settings as follows我通过使用 DATABASES 设置解决了这个问题,如下所示
DATABASES = {
'default': {
'ENGINE': 'transaction_hooks.backends.mysql',
.
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.