jackfrued / Python-100-Days

Python - 100天从新手到大师
157.55k stars 52.44k forks source link

42.深入模型 第3点的时候,继续不下去了。 #201

Open xiaoqiangqiang2019 opened 5 years ago

xiaoqiangqiang2019 commented 5 years ago

drop database if exists oa; create database oa default charset utf8;

这个根本不知道哪里执行。

python manage.py migrate 执行后面的,也报错了。 (base) D:\work\study\201906\6.17>python manage.py migrate Traceback (most recent call last): File "manage.py", line 21, in main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "D:\ProgramData\Anaconda3\lib\site-packages\django\core\management__init .py", line 381, in execute_from_command_line utility.execute() File "D:\ProgramData\Anaconda3\lib\site-packages\django\core\management__init .py", line 357, in execute django.setup() File "D:\ProgramData\Anaconda3\lib\site-packages\django\init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "D:\ProgramData\Anaconda3\lib\site-packages\django\apps\registry.py", lin e 114, in populate app_config.import_models() File "D:\ProgramData\Anaconda3\lib\site-packages\django\apps\config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "D:\ProgramData\Anaconda3\lib\importlib\init__.py", line 127, in import _module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File "D:\ProgramData\Anaconda3\lib\site-packages\django\contrib\auth\models.py ", line 2, in from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "D:\ProgramData\Anaconda3\lib\site-packages\django\contrib\auth\base_user .py", line 47, in class AbstractBaseUser(models.Model): File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\models\base.py", li ne 117, in new new_class.add_to_class('_meta', Options(meta, app_label)) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\models\base.py", li ne 321, in add_to_class value.contribute_to_class(cls, name) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\models\options.py", line 204, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length( )) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\init.py", line 28, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\utils.py", line 201 , in getitem backend = load_backend(db['ENGINE']) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\utils.py", line 110 , in load_backend return import_module('%s.base' % backend_name) File "D:\ProgramData\Anaconda3\lib\importlib\init.py", line 127, in import _module return _bootstrap._gcd_import(name[level:], package, level) File "D:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\base .py", line 36, in raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you hav e %s.' % Database.version__) django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is requ ired; you have 0.9.3.

zhangxingguo1992 commented 5 years ago

mysql要升级吧

xiaoqiangqiang2019 commented 5 years ago

mysql要升级吧

是么? 那前面那个创建数据库的语句要这么执行呢? 查了半天,没查到相关资料!

luswangl2013 commented 5 years ago

create database oa default charset utf8; 这要下载并开启mysql并在mysql语句中执行。 我也是小白入门,在这个地方琢磨了好久

xiaoqiangqiang2019 commented 5 years ago

create database oa default charset utf8; 这要下载并开启mysql并在mysql语句中执行。 我也是小白入门,在这个地方琢磨了好久

哦,难怪,只是mysql我查了好多资料,都没有在终端上操作,都是在python的文件代码里操作。

luswangl2013 commented 5 years ago

create database oa default charset utf8; 这要下载并开启mysql并在mysql语句中执行。 我也是小白入门,在这个地方琢磨了好久

哦,难怪,只是mysql我查了好多资料,都没有在终端上操作,都是在python的文件代码里操作。

还是有的。下载mysql后有个workbench,可以在里面写mysql语句。网上有教程的

xiaoqiangqiang2019 commented 5 years ago

create database oa default charset utf8; 这要下载并开启mysql并在mysql语句中执行。 我也是小白入门,在这个地方琢磨了好久

哦,难怪,只是mysql我查了好多资料,都没有在终端上操作,都是在python的文件代码里操作。

还是有的。下载mysql后有个workbench,可以在里面写mysql语句。网上有教程的

终于搞明白了,pymysql这只是一个用来连接mysql的库,并不是mysql,吐血。 折腾了几天,才明白。 起个这么诱惑的名字。 教程也不说明下,差评!!!

hhxadhy commented 5 years ago

命令行 create database oa default charset utf8; 要在mysql commamd line 里面执行。

版本问题是,pymysql 只是用于 python连接mysql的工具,这里提示版本问题,我是更换了mysqlclient来操作。只需要在 init 文件里import MySQLdb

Lim-LinYuanbo commented 5 years ago

有具体步奏吗?不然还有很多人继续走弯路

xiaokeliu666 commented 5 years ago

我遇到了和你一样的问题,现在已经解决,你可以尝试一下我的方法,希望对你有帮助。 进入路径"D:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\base .py",搜索 "version = Database.version_info",然后把if version < (1, 3, 13)和下面那一行都注释掉,或者加个pass。 然后再搜索"query = query.decode(errors='replace')",把decode改成encode: query = query.encode(errors='replace') 最后再尝试迁移。 P.S. 我的版本是python3.6+Django2