Django項目升級到2.2後報錯的解決過程

用pip進行更新後

在這裏插入圖片描述

pip install --upgrade Django==2.2

一堆報錯

  1. django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
    解決方法:
    找到 python目錄下/site-packages/django/db/backends/mysql/base.py,註釋掉下方的兩行代碼
    在這裏插入圖片描述

  2. TypeError: init() missing 1 required positional argument: ‘on_delete’
    解決方法:
    表與表之間關聯的時候,必須要寫on_delete參數
    在這裏插入圖片描述

on_delete參數的各個值的含義:

on_delete=None,               # 刪除關聯表中的數據時,當前表與其關聯的field的行爲
on_delete=models.CASCADE,     # 刪除關聯數據,與之關聯也刪除
on_delete=models.DO_NOTHING,  # 刪除關聯數據,什麼也不做
on_delete=models.PROTECT,     # 刪除關聯數據,引發錯誤ProtectedError

# models.ForeignKey('關聯表', on_delete=models.SET_NULL, blank=True, null=True)
on_delete=models.SET_NULL,    # 刪除關聯數據,與之關聯的值設置爲null(前提
FK字段需要設置爲可空,一對一同理)

# models.ForeignKey('關聯表', on_delete=models.SET_DEFAULT, default='默認值')
on_delete=models.SET_DEFAULT, # 刪除關聯數據,與之關聯的值設置爲默認值(前提
FK字段需要設置默認值,一對一同理)

on_delete=models.SET,         # 刪除關聯數據,
 a. 與之關聯的值設置爲指定值,設置:models.SET()
 b. 與之關聯的值設置爲可執行對象的返回值,設置:models.SET(可執行對象)
  1. ImportError: cannot import name ‘RegexURLResolver’ from ‘django.urls’ (D:\Anaconda3\envs\scrapy\lib\site-packages\django\urls_init_.py)
    在這裏插入圖片描述
    解決方式:把下面註釋掉的代碼換爲第二行的代碼
# from django.urls import RegexURLResolver, RegexURLPattern
from django.urls.resolvers import URLResolver, URLPattern
  1. django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
    在這裏插入圖片描述
    解決方法:django.urls中include函數第一個參數傳入的是tuple類型,其中一個是參數是app_name,且app_name需賦值,在include中改爲下面的代碼
include(('rbac.urls','rbac'), namespace='rbac')
  1. query = query.decode(errors=‘replace’)AttributeError: ‘str’ object has no attribute ‘decode’
    解決方法:
    把decode改爲encode
    在這裏插入圖片描述

ko

發現還是飄紅
在這裏插入圖片描述
虛驚一場
在這裏插入圖片描述

至此,項目成功更新到2.2

感謝此篇博文的部分解惑:https://blog.csdn.net/Arry_Lee/article/details/100746633

發佈了41 篇原創文章 · 獲贊 9 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章