3

当我尝试查看现有对象的列表时,我在管理页面上收到以下错误。

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

我做错了什么?

4

1 回答 1

12

__unicode__应该返回unicode

def __unicode__(self):
    return u'{c}/{l}/{p}'.format(c=self.char.name, l=self.level, p=self.prof)
于 2013-02-21T14:50:09.240 回答