今天我通过 Django 管理员收到了无法编码的数据。不知何故,数据的编码不是 unicode。这怎么可能?
我的模型中有一个name
属性,Client
它以 unicode 格式返回数据:
@property
def name(self):
return u'{0} {1}'.format(self.firstname, self.lastname).strip()
但这不起作用:
>>> client
<Client: [Bad Unicode data]>
>>> client.lastname
'Dani\xc3\xabl'
>>> client.lastname.__class__
<type 'str'>
>>> u"{0} {1}".format(client.firstname, client.lastname)
Traceback (most recent call last):
File "<console>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)
足够奇怪,将名字/姓氏编码为常规字符串确实有效:
>>> "{0} {1}".format(client.firstname, client.lastname)
'Test Dani\xc3\xabl'
>>> "{0} {1}".format(client.firstname, client.lastname).decode('utf-8')
u'Test Dani\xebl'
这里发生了什么?这个输入是如何通过管理员进入我的模型的?
系统堆栈(它是一个外部服务器):
- Debian 6.0.5(挤压)
- Django 1.4.1
- Python 2.6.6
- MySQL 5.1.49
- MySQL-python==1.2.2
这是相关的模型代码:
class Client(models.Model):
firstname = models.CharField(_("Firstname"), max_length=255)
lastname = models.CharField(_("Lastname"), max_length=255)
email = models.EmailField(_("Email"), unique=True, max_length=255)
class Meta:
db_table = u'clients'
ordering = ('firstname', 'lastname', 'email')
def __unicode__(self):
return u'{0} <{1}>'.format(self.name, self.email)
@property
def name(self):
return u'{0} {1}'.format(self.firstname, self.lastname).strip()