django+mysql='DatabaseWrapper'对象没有属性 'Database'错误 [英] django+mysql='DatabaseWrapper' object has no attribute 'Database' error

查看:77
本文介绍了django+mysql='DatabaseWrapper'对象没有属性 'Database'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了 Python 3.3.0mysql-connectorDjango.然后我创建了我的第一个应用程序 mysite.在 settings.py 中,我添加了以下几行:

I've just installed Python 3.3.0, mysql-connector and Django. Then I created my first application called mysite. In settings.py I added these lines:

DATABASES = {
'default': {
    'ENGINE': 'mysql.connector.django', 
    'NAME': 'mydb',
    'USER': 'root',
    'PASSWORD': 'root',
    'HOST': 'localhost',
    'PORT': '3306',
}
}

当我运行服务器并进入管理页面 127.0.0.1:8000/admin/ 时,我看到一长串以 AttributeError at/admin/'DatabaseWrapper' 对象开头的错误没有属性数据库".我不知道如何处理所有这些东西.完整的错误描述是:

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'. 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']

编辑

当我运行 python manage.py syncdb 时,我也得到一长串错误:

When I run python manage.py syncdb, I also get a long list of errors:

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

推荐答案

好的,所以我遇到了完全相同的问题.关于如何修改现有库的一部分以使 Python3 与 MySQL 一起工作有很多建议,但我没有发现它们中的任何一个可以 100% 工作.我无法使官方 MySQL Python 连接器与 Django 和 Python 3.3 一起使用.

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%. I wasn't able to make official MySQL Python connector to work with Django and Python 3.3.

起作用的是切换到 PyMySQL 库.几个月前我已经尝试过了,但当时它对我不起作用.现在,有一个开箱即用的新版本 0.6.1.所以,还有更多细节:

What did work was switching to PyMySQL library instead. 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. So, few more details:

我的环境:OSX 10.9、Python 3.3.3、Django 1.6.1、MyPySQL 0.6.1、Windows 上的 MySQL Server 5.5

My environment: OSX 10.9 , Python 3.3.3, Django 1.6.1, MyPySQL 0.6.1, MySQL Server 5.5 on Windows

如何让它工作:

How to make it work:

  1. 安装 PyMySQL 0.6.1 版 (https://github.com/PyMySQL/PyMySQL/):您可以使用pip安装它,即:pip install PyMySQL或手动下载软件包;在他们的网站上有一个很好的文档说明如何做到这一点.

  1. Install PyMySQL version 0.6.1 (https://github.com/PyMySQL/PyMySQL/): you can install it either by using pip, i.e. : pip install PyMySQL or by manually downloading the package; there is a good documentation on their website on how to do that.

打开您的 Django 应用 __init__.py 并粘贴以下几行:

Open your Django App __init__.py and paste the following lines:

import pymysql
pymysql.install_as_MySQLdb() 

  • 现在,打开 settings.py 并确保您的 DATABASE 属性如下所示:

  • Now, open settings.py and make sure your DATABASE property looks like this:

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'mydb',
           'USER': 'dbuser',
           'PASSWORD': 'dbpassword',
           'HOST': 'dbhost',
           'PORT': '3306'
        }
    }
    

  • 就是这样,你应该能够执行python manage.py syncdb,然后初始化你的MySQL数据库;请参阅下面的示例输出:

  • That's it, you should be able to execute python manage.py syncdb so init your MySQL DB; 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.
    ...
    

  • 这篇关于django+mysql=&amp;#39;DatabaseWrapper&amp;#39;对象没有属性 &amp;#39;Database&amp;#39;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆