關於Django出現“__str__ returned non-string (type NoneType)”錯誤的處理


跟着cls超哥學django,只不過我用的版本是python3.85,Django版本為3.1.2。我總是想用最新的東西,盡管為此付出了不小的代價。噢,還有我是在Deepin20下學習。
仔細檢查發現代碼沒有什么錯誤,花了我好多時間。不過這好多時間,都是不斷測試我的模板語法是否有錯。

Error during template rendering
In template /home/zyl07/PycharmProjects/SCRM/sales/templates/sales_html/starter.html, error at line 12

因為,這兩行錯誤信息太容易看到了!前前后后,折騰了一個下午,還是沒有解決。沒辦法,只好靜下心來,仔細看了錯誤提示:

TypeError at /sales/add_customer/
__str__ returned non-string (type NoneType)
Request Method:	GET
Request URL:	http://127.0.0.1:8000/sales/add_customer/
Django Version:	3.1.2
Exception Type:	TypeError
Exception Value:	
__str__ returned non-string (type NoneType)
Exception Location:	/home/zyl07/djs/djs3/lib/python3.8/site-packages/django/forms/models.py, line 1240, in label_from_instance
Python Executable:	/home/zyl07/djs/djs3/bin/python
Python Version:	3.8.5
Python Path:	
['/home/zyl07/PycharmProjects/SCRM',
 '/home/zyl07/PycharmProjects/SCRM',
 '/home/zyl07/pycharm/pycharm-2020.2/plugins/python/helpers/pycharm_display',
 '/usr/local/lib/python38.zip',
 '/usr/local/lib/python3.8',
 '/usr/local/lib/python3.8/lib-dynload',
 '/home/zyl07/djs/djs3/lib/python3.8/site-packages',
 '/home/zyl07/pycharm/pycharm-2020.2/plugins/python/helpers/pycharm_matplotlib_backend']
Server time:	Mon, 19 Oct 2020 19:57:22 +0800

我發現異常出現在這個位置:
Exception Location: /home/zyl07/djs/djs3/lib/python3.8/site-packages/django/forms/models.py, line 1240, in label_from_instance
打開源文件:

    def label_from_instance(self, obj):
        """
        Convert objects into strings and generate the labels for the choices
        presented by this object. Subclasses can override this method to
        customize the display of the choices.
        """
        return str(obj)

這只不過是某一個類的一個方法。要往前追代碼太困難了,再說,我也不定看得懂。於是用了一個笨辦法。

    def label_from_instance(self, obj):
        """
        Convert objects into strings and generate the labels for the choices
        presented by this object. Subclasses can override this method to
        customize the display of the choices.
        """
        print(type(obj))
      # 下面這一句主要是為了顯示明顯!
        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
        return str(obj)

第一次修改Pycharm還提示,你是否真得要修改。果斷修改,反正我還能改回來。重啟服務器,刷新頁面。天哪,看出現了什么 ?

<class 'sales.models.Customer'>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<class 'sales.models.Customer'>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<class 'sales.models.Customer'>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<class 'sales.models.Customer'>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<class 'sales.models.Customer'>

出現了一片這個玩意兒。這樣,我就知道目標在哪兒了。其實問題就出在我的Customer類中有一個name字段,是可以為空的,也就是說可以不填數據。
但是下面的__str__方法中,我卻將這個字段返回。所以,就造成了returned non-string (type NoneType)這樣的錯誤。本為應該返回字符串,卻返回了一個None類型。
找到了問題,解決辦法就有了,要不你返回其他字段,不可以為空的字段;要不加上個判斷,如果為None時,返回一個特定的字符串就可以了。

    def __str__(self):
        if self.name:
            return self.name
        else:
            return "未留姓名"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM