美文网首页
django项目从python2.7升级为python3.6踩坑

django项目从python2.7升级为python3.6踩坑

作者: xingxixi | 来源:发表于2019-11-28 14:08 被阅读0次

    问题一 reload(sys)报错

    解决方法:

    import importlib
    importlib.reload(sys)
    

    问题二 django-simple-serializer库安装失败

    下载django-simple-serializer-2.0.7.tar.gz,解压后,修改setup.py文件:

    import importlib
    importlib.reload(sys)
    LONG_DESCRIPTION =open('README.rst','r', encoding='UTF-8').read()
    

    运行python setup.py install,成功。

    问题三 数据库报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    注释掉检测数据库版本的代码(packages/django/db/backends/mysql/base.py", line 36, in <module>):

    # if version < (1, 3, 13):
    #     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
    

    问题四 运行报错AttributeError: 'str' object has no attribute 'decode'

    解决办法:打开"\lib\site-packages\django\db\backends\mysql\operations.py"文件把146行的decode修改为encode即可。

     if query is not None:
         query = query.decode(errors='replace')
    

    问题五 django-simple-serializer库,使用报错'AutoField' object has no attribute 'rel'

    pro_id_name = serializer(projects, include_attr=("id", "name", "project_abbr", "latest_ver"))
    问题起因:在django1.9及之后版本,autoField字段类型使用remote_field而不是ref,需要做一下兼容。
    解决方法:https://github.com/bluedazzle/django-simple-serializer/issues/8
    修改django_simple_serializer-2.0.7-py3.6.egg包里的Serializer.py文件,函数data_inspect里增加判断:

    for field in concrete_model._meta.local_fields:
                    # 检查 field 是否存在 rel 这个属性,为'AutoField' object has no attribute 'rel'错误填坑
                    if hasattr(field, 'rel'):
                        if field.rel is None:
                            if self.check_attr(field.name) and hasattr(data, field.name):
                                obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
                        else:
                            if self.check_attr(field.name) and self.foreign:
                                obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
                    else:
                        if self.check_attr(field.name) and hasattr(data, field.name):
                            obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
    

    相关文章

      网友评论

          本文标题:django项目从python2.7升级为python3.6踩坑

          本文链接:https://www.haomeiwen.com/subject/lwpfwctx.html