且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django+mysql='DatabaseWrapper'对象没有属性 'Database'错误

更新时间:2021-11-30 21:08:37

好的,所以我遇到了完全相同的问题.关于如何修改现有库的一部分以使 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.
    ...