問題
- 平時在生成migrate檔案時,如果出現問題,一般都會顯示失敗的原因,造成失敗的是哪個字段,但是也有可能會出現這種情況:
File "/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 148, in state_forwards delay = not old_field.is_relation AttributeError: 'NoneType' object has no attribute 'is_relation'
- 這樣僅僅看報錯的資訊就沒辦法準确的定位自己失敗的原因了。
解決方法
- 修改檔案
。環境路徑 + /lib/python3.5/site-packages/django/db/migrations/operations/fields.py
'NoneType' object has no attribute 'is_relation',報錯原因定位 - 打開檔案,根據報錯提示,找到第148行,在
方法中加入以下代碼:state_forwards
print(app_label + " " + self.model_name_lower + " " + self.name)
- 再次執行makemigrations。就會列印出執行時makemigrations走到了哪一個字段。
- 然後就可以根據報錯前列印的字段名,定位到問題字段,這樣就可以更快的查到錯誤原因了。
參考連結:
Django makemigrations 報錯原因定位