django+mysql='DatabaseWrapper' object has no attribute 'Database' error

前端 未结 2 639
别那么骄傲
别那么骄傲 2021-01-24 01:51

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

相关标签:
2条回答
  • 2021-01-24 02:37

    I had same error for Django 1.8.9, Python 3.6.12. And I was using django-transaction-hooks==0.2. And my DATABASES settings were

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

    I fixed the issue by using DATABASES settings as follows

    DATABASES = {
    'default': {
        'ENGINE': 'transaction_hooks.backends.mysql',
        .
        .
    
    0 讨论(0)
  • 2021-01-24 02:39

    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.

    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:

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

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

      import pymysql
      pymysql.install_as_MySQLdb() 
      
    3. 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'
          }
      }
      
    4. 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.
      ...
      
    0 讨论(0)
提交回复
热议问题