把django项目git clone后本地pycharm运行总是很多东西需要配置,感觉可能在项目弄个虚拟环境就最方便了,即项目下的venv文件夹。(能力有限,设置不好还是会运行到本地的python或Anaconda3的资源。
经常出问题的一定就是数据库了,不同环境下mysql数据库的配置基本不一样呢。
执行 python manage.py makemigrations
报错:
1. 缺少module
ModuleNotFoundError: No module named 'pymysql'
解决:直接pip install pymysql即可
2. mysqlclient版本不符合
File "D:\SOFTWARE\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py",line 36, in <module>
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决:将提示目录的base.py文件对应行注释掉
3. ‘str’ object has no attribute ‘decode’
File "D:\SOFTWARE\Anaconda3\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
str通过encode()编码变成byte,反之decode()译码
解决:将提示目录的operations.py文件对应的decode改为encode
4.数据库安全模式
D:\SOFTWARE\Anaconda3\lib\site-packages\pymysql\cursors.py:170: Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.")
result = self._query(query)
No changes detected
解决:把项目的settings.py关于数据的option注释掉
其他
- 某些问题下,数据库迁移不成功可以把迁移文件删掉重新迁移
- 迁移命令
python manage.py makemigrations
python manage.py check # 检查是否错误
python manage.py migrate