可能是由于Django使用的MySQLdb庫對Python3不支持,我們用采用了PyMySQL庫來代替,導致出現(xiàn)各種坑,特別是執(zhí)行以下2條命令的是時候:
python manage.py makemigrations
or
python manage.py inspectdb
無聊你是否執(zhí)行pip install mysqlclient安裝的最新版的,都拋出:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None
MD,LZ看到這錯誤太想罵人了,沒辦法采取網(wǎng)上的方法,注釋大法!
找到Python安裝路勁下的Python36-32\Lib\site-packages\django\db\backends\mysql\base.py文件
將文件中的如下代碼注釋(可能需先關(guān)閉pycharm IDE)
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
對對對,py3默認str是unicode編碼,通過encode方法編碼成bytes類型,后者才有decode解碼方法。
提示錯誤來源:Python36\lib\site-packages\django\db\backends\mysql\operations.py", line 149, in last_executed_query
源方法內(nèi)容(pip安裝的django 2.2.1原封不動的內(nèi)容):
def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"
# attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution.
query = getattr(cursor, '_executed', None)
if query is not None:
query = query.decode(errors='replace')
return query
通過print大法輸出query結(jié)果,內(nèi)容為
SELECT @@SQL_AUTO_IS_NULL
數(shù)據(jù)類型為str
于是我去django的github去翻這個文件這個方法的最新/歷史版本,結(jié)果最新master分支內(nèi)容如下:
def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"
# attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution.
# MySQLdb returns string, PyMySQL bytes.
return force_str(getattr(cursor, '_executed', None), errors='replace')
看函數(shù)名,應(yīng)該是強制去把SQL轉(zhuǎn)換成str了
我靠?。?!這尼瑪官網(wǎng)2.2.1/2.2.2(當前最新版)的包不是害人么,記得該文件上面引入下這個方法
from django.utils.encoding import force_str
然后再執(zhí)行managa.py命令,可以了
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
名稱欄目:幾個django2.2和mysql使用的坑-創(chuàng)新互聯(lián)
當前路徑:http://chinadenli.net/article14/dcpode.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站設(shè)計、網(wǎng)站導航、Google、域名注冊、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容