django項(xiàng)目從python2.7升級(jí)為python3.6踩坑記錄

問題一 reload(sys)報(bào)錯(cuò)

解決方法:

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()

運(yùn)行python setup.py install,成功。

問題三 數(shù)據(jù)庫報(bào)錯(cuò)django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

注釋掉檢測(cè)數(shù)據(jù)庫版本的代碼(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__)

問題四 運(yùn)行報(bào)錯(cuò)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庫,使用報(bào)錯(cuò)'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文件,函數(shù)data_inspect里增加判斷:

for field in concrete_model._meta.local_fields:
                # 檢查 field 是否存在 rel 這個(gè)屬性,為'AutoField' object has no attribute 'rel'錯(cuò)誤填坑
                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))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容