django BUG!!! === Django model "coercing to Unicode: need string or buffer, XXX found"


coercing to Unicode: need string or buffer, datetime.datetime found
coercing to Unicode: need string or buffer, int found

在models中,__unicode__(self) return self.datetime ------> return "%s" % self.datetime
                  return self.int ---------->return "%d" % self.int

def __unicode__(self):
return self.title
__unicode__() 方法可以進行任何處理來返回對一個對象的字符串表示。 Publisher和Book對象的__unicode__()方法簡單地返回各自的名稱和標題,
Author對象的__unicode__()方法則稍微復雜一些,它將first_name和last_name字段值以空格連接后再返回。
對__unicode__()的唯一要求就是它要返回一個unicode對象 如果`` __unicode__()`` 方法未返回一個Unicode對象,而返回比如說一個整型數字,
那么Python將拋出一個`` TypeError`` 錯誤,並提示:”coercing to Unicode: need string or buffer, int found” 。
unicode對象就是一個Python字符串.
Unicode對象並沒有編碼。它們使用Unicode,一個一致的,通用的字符編碼集。 當你在Python中處理Unicode對象的時候,你可以直接將它們混合使用和互相匹配而不必去考慮編碼細節。
Django 在其內部的各個方面都使用到了 Unicode 對象。 模型 對象中,檢索匹配方面的操作使用的是 Unicode 對象,視圖 函數之間的交互使用的是 Unicode 對象,
模板的渲染也是用的 Unicode 對象。通常,我們不必擔心編碼是否正確,后台會處理的很好。
為了讓我們的修改生效,先退出Python Shell,然后再次運行 python manage.py shell 進入。(這是保證代碼修改生效的最簡單方法。)
>>> publisher_list = Publisher.objects.all()
>>> publisher_list
[<Publisher: Apress>]


免責聲明!

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



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