当我尝试查看现有对象列表时,我在管理页面上收到以下错误。
UnicodeEncodeError at /admin/character/charlevel/
'ascii' codec can't encode character u'\xd6' in position 0: ordinal not in range(128)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/character/charlevel/
Django Version: 1.4.1
Exception Type: UnicodeEncodeError
Exception Value:
'ascii' codec can't encode character u'\xd6' in position 0: ordinal not in range(128)
Exception Location: /home/***/workspace/***/***/character/models.py in __unicode__, line 413
Python Executable: /usr/bin/python
Python Version: 2.7.3
当我打开这个类的对象列表时会发生这种情况:
class CharLevel(models.Model):
char = models.ForeignKey(Character)
prof = models.ForeignKey(Profession)
level = models.SmallIntegerField()
def __unicode__(self):
return ('{c}/{l}/{p}'.format(c=self.char.name, l=self.level, p=self.prof )).encode('utf-8')
如果我删除字符串格式的{c}
组件
但是,Charater类不会出现以下__unicode__
:
class Character(models.Model):
name = models.CharField(max_length=32)
def __unicode__(self):
return self.name
我做错了什么?
答案 0 :(得分:12)
__unicode__
应该返回unicode
:
def __unicode__(self):
return u'{c}/{l}/{p}'.format(c=self.char.name, l=self.level, p=self.prof)