我有一个如下所示的模型类:
class Address(models.Model):
# taking length of address/city fields from existing UserProfile model
address_1 = models.CharField(max_length=128,
blank=False,
null=False)
address_2 = models.CharField(max_length=128,
blank=True,
null=True)
address_3 = models.CharField(max_length=128,
blank=True,
null=True)
unit = models.CharField(max_length=10,
blank=True,
null=True)
city = models.CharField(max_length=128,
blank=False,
null=False)
state_or_province = models.ForeignKey(StateOrProvince)
postal_code = models.CharField(max_length=20,
blank=False,
null=False)
phone = models.CharField(max_length=20,
blank=True,
null=True)
is_deleted = models.BooleanField(default=False,
null=False)
def __unicode__(self):
return u"{}, {} {}, {}".format(
self.city, self.state_or_province.postal_abbrev, self.postal_code, self.address_1)
关键是 __unicode__
方法。我有一个客户模型,该模型具有该表的外键字段,并且我正在执行以下日志记录:
log.debug(u'Generated customer [{}]'.format(vars(customer)))
这工作正常,但如果 address_1 字段值包含非 ascii 值,例如
57562 Vån Ness Hwy
系统抛出以下异常:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 345: ordinal not in range(128)
我在 django/db/models/base.py 中找到了一个奇怪的方法:
def __repr__(self):
try:
u = six.text_type(self)
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
return force_str('<%s: %s>' % (self.__class__.__name__, u))
如您所见,此方法被调用到force_str,但未正确处理。这是一个错误吗?如果在我的对象上调用 unicode,那么所有内容不都应该是 unicode 吗?
最佳答案
根据docs ,当 python 对象作为参数传递给 '{}'.format(obj)
时,
A general convention is that an empty format string ("") [within the "{}"] produces the same result as if you had called str() on the value.
这意味着您实际上正在调用 str(vars(customer))
,和vars(customer)
返回 dict
.
调用str()
在dict
上会打电话repr()
其键和值,否则您会得到不明确的输出(例如 str(1) == str('1') == '1'
但 repr(1) == '1' and repr('1') == '"1"'
(请参阅 Difference between __str__ and __repr__ in Python )
因此repr()
仍在调用您的Address
,它返回一个字符串。
现在从 repr()
返回 unicode Python 2 中不允许 - https://stackoverflow.com/a/3627835/648176 ,因此您需要覆盖 __str__()
在您的模型中使其处理解码为 ascii ( Django docs ),或执行类似以下操作:
string_dict = {str(k): str(v) for (k, v) in vars(customer).items()}
log.debug(u'Generated customer [{}]'.format(string_dict))
关于Django 模型 __unicode__ 在记录时引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32193797/