指定使用pymysql连接数据库
现在django官方建议使用的连接MySQL的方法是mysqlclient,但是直接pip的话会报错,这东西有C的运行库依赖,需要在机器上装一个MySQL,不想装的话就可以指定使用pymysql。
首先在工程的setting.py里面把DATABASE改成使用MySQL,这里需要注意使用的用户需要有数据库的很多权限。
然后在init.py里面加上上面的内容
import pymysql
pymysql.install_as_MySQLdb()
然后执行migrate的时候大概率会报两个错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
AttributeError: ‘str’ object has no attribute ‘decode’
这时候需要去python安装路径下的site-packages/django/db/backends/mysql/这个文件夹里修改base.py和opeartions.py两个文件
# 在base.py里注释35和36行的内容
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
# 在operations.py里把146行的decode改为encode
if query is not None:
query = query.decode(errors='replace')
使用已有的数据库
这个在官方文档里给出了方法:
配置了数据库相关内容以后,执行
python manage.py inspectdb
将产生的python代码放入models.py,然后migrate就行了