Can I use MySQL on Django(dev 1.6.x) with Python3.x?

后端 未结 4 580
不思量自难忘°
不思量自难忘° 2020-12-04 16:36

I use Django dev(1.6.x) from git repo and I want to use MySQL , But on the settings.py file can not setup MySQL because MySQL is not supported in python3 and Dj

4条回答
  •  一整个雨季
    2020-12-04 17:28

    I also struggled with making MySQL work with Django 1.6 and Python 3.3; the only thing that worked was to switch to PyMySQL. See my post on that here

    Adding the answer below

    My environment: OSX 10.9, Python 3.3.3, Django 1.6.1, PyMySQL 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 to 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.
      ...
      

提交回复
热议问题