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

时间:2014-01-03 16:20:42

标签: python mysql django

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

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' object has no attribute 'Database'开头的错误。我不知道该怎么做这些东西。完整的错误描述是:

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时,我也会得到一长串错误:

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

2 个答案:

答案 0 :(得分:1)

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

改变了PyMySQL库的工作是什么。几个月前我已经尝试过了,但那时候它对我不起作用。现在,有一个新版本,0.6.1开箱即用。所以,更多细节:

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

如何使其发挥作用:

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

  2. 打开您的Django应用 __ init __。py 并粘贴以下行:

    import pymysql
    pymysql.install_as_MySQLdb() 
    
  3. 现在,打开 settings.py 并确保您的DATABASE属性如下所示:

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'mydb',
           'USER': 'dbuser',
           'PASSWORD': 'dbpassword',
           'HOST': 'dbhost',
           'PORT': '3306'
        }
    }
    
  4. 就是这样,你应该能够执行python manage.py syncdb所以初始化你的MySQL数据库;请参阅下面的示例输出:

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

答案 1 :(得分:0)

我在 Django 1.8.9、Python 3.6.12 上遇到了同样的错误。而我使用的是 django-transaction-hooks==0.2。我的数据库设置是

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    .
    .

我使用如下的 DATABASES 设置解决了这个问题

DATABASES = {
'default': {
    'ENGINE': 'transaction_hooks.backends.mysql',
    .
    .