如果你的Django是最新的2.2,PyMySQL也是最新的0.93的话,生成迁移文件时你会发现Django会报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
这时候,我们编辑一下base.py文件,注释掉其中版本检测的代码,如下图所示,base.py文件就是报错中提示的有这个文件的路径,编辑之后的base.py文件:
接下来执行迁移命令,发现又有错误,
AttributeError: ‘str’ object has no attribute ‘decode’
,根据提示,在报错中找到那个路径,就是下图中operations.py文件,进去编辑,找到decode,改成encode。
引用外键时,
TypeError: init() missing 1 required positional argument: ‘on_delete’
需要在引用外键值后添加 on_delete=models.CASCADE
之后就成功运行了
给项目授权连接远程数据库
grant all privileges on dailyfresh to ‘root(账号)’@‘169.254.225.254’ identified by ‘root(密码)’ with grant option;
flush privileges;
django.db.utils.OperationalError: (2003, “Can’t connect to MySQL server on ‘192.168.223.128’ ([WinError 10061] 由于目标计算机积极拒绝,无法连接。)”)
cd /etc/mysql/mysql.conf.d/
vim mysqld.cnf
bind-address =127.0.0.1 只能本地访问
bind-address =0.0.0.0 不限制访问