Search code examples
mysqldjangopython-3.xmysql-pythonpymysql

how to configure django with mysql using pymysql (python 3.5)


having much trouble getting mysql(5.6) to connect with django (1.11) using python 3.5.2 in ubuntu(16.04) venv. im trying to use pymysql because it works great with flask applications, just not django yet. mysqlclient failed install and has other dependencies (ive read django docs yes) so would prefer avoiding that module. this really should work since it works flawless in flask and others seem to have it working. heres what Ive tried and found:

# first steps

pip install pymysql

mysql -u root -p
CREATE DATABASE djang0 CHARACTER SET utf8 COLLATE utf8_bin;


# manage.py

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

# settings.py

DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'djang0',
        'USER': 'root',
        'PASSWORD': 'mypasswd',
        'HOST': '',
        'PORT': '3306',
    }
}


# current models.py Post class for blog

class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField()
date = models.DateTimeField()
image = models.ImageField(upload_to='uploads', blank=True)


# error - full here https://dpaste.de/vtEH

[Thu Apr 27 18:58:00.010964 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
[Thu Apr 27 18:58:00.010967 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] Did you install mysqlclient or MySQL-python?

Solution

  • had to use dependency, pip install mysqlclient. boo