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 に答える