2017-03-18 23 views
0

這是我的海峽和unicode ...脅迫到Unicode:需要字符串或緩衝區,NoneType發現一切似乎很動聽,仍然收到此錯誤

def __str__(self): 
    return '%s' % self.id 

def __unicode__(self): 
    return self.title or u'' 

,我已經嘗試過這...

def __str__(self): 
    return self.id 

def __unicode__(self): 
    return self.id 

仍然收到這個錯誤..

coercing to Unicode: need string or buffer, NoneType found 

not able to figure it out

+0

下面的答案是否適合您? –

回答

1

首先您不必實現這兩種方法。如果您使用的是python 2,則使用__unicode__。如果您使用python 3,請使用__str__

所以,使用python 2做到這一點:

def __unicode__(self): 
    return '{}'.format(self.id) 

爲Python 3裏這樣做:

def __str__(self): 
    return '{}'.format(self.id) 
0

我已經得到了答案.....但日Thnx烏拉圭回合rply

def __str__(self): 
    return str(self.id) 

def __unicode__(self): 
    return unicode(self.id) 
相關問題