Python 在Django模型上仅使用_unicode__)方法调用str()会为特殊字符返回不同的编码
在返回字段中带有特殊字符的Django模型对象上调用Python 在Django模型上仅使用_unicode__)方法调用str()会为特殊字符返回不同的编码,python,django,django-1.5,Python,Django,Django 1.5,在返回字段中带有特殊字符的Django模型对象上调用str()时,在这种情况下,字符é将返回为\xc3\xa9,而不是预期的\xe9。有什么问题 我正在使用Django 1.5.1和#coding=utf-8和来自uuuu future\uuuuuuu导入unicode\u文本as 我只实现了\uuuuuunicode\uuuu方法,而没有实现\uuuuu str\uuuu方法 下面是一个代码示例(不是真正的生产代码) tests.py # coding=utf-8 from __future_
str()
时,在这种情况下,字符é将返回为\xc3\xa9
,而不是预期的\xe9
。有什么问题
我正在使用Django 1.5.1和#coding=utf-8
和来自uuuu future\uuuuuuu导入unicode\u文本
as
我只实现了\uuuuuunicode\uuuu
方法,而没有实现\uuuuu str\uuuu
方法
下面是一个代码示例(不是真正的生产代码)
tests.py
# coding=utf-8
from __future__ import unicode_literals
from django.test import TestCase
from unicodebug.models import MyModel
class TestMyModel(TestCase):
def test_mymodel_str(self):
mymodel = MyModel(name='Chéri')
self.assertEqual(str(mymodel), 'Chéri')
def test_mymodel_unicode(self):
mymodel = MyModel(name='Chéri')
self.assertEqual(unicode(mymodel), 'Chéri') # using unicode() works fine!
models.py
# coding=utf-8
from __future__ import unicode_literals
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
运行测试时在终端中输出
(venv)frecon test$ python manage.py test unicodebug
Creating test database for alias 'default'...
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py:501: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if not first == second:
F.
======================================================================
FAIL: test_mymodel_str (unicodebug.tests.TestMyModel)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unicodebug/tests.py", line 10, in test_mymodel_str
self.assertEqual(str(mymodel), 'Chéri')
AssertionError: 'Ch\xc3\xa9ri' != u'Ch\xe9ri'
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
Destroying test database for alias 'default'...
str()
必须对unicode值进行编码以生成Pyton字节字符串,它将使用默认编码(sys.defaultencoding
)进行编码。您必须明确地将后者设置为UTF8
,因为默认情况下,后者设置为ASCII
您看到的是模型返回的Unicode值的UTF-8编码。如果希望测试通过,则必须对该值进行解码,以与要比较的Unicode值匹配,或者对其他值进行编码:
self.assertEqual(str(mymodel), 'Chéri'.encode('utf8'))
或
简短演示:
>>> from __future__ import unicode_literals
>>> b'Ch\xc3\xa9ri' == 'Chéri'.encode('utf8')
True
>>> b'Ch\xc3\xa9ri'.decode('utf8') == 'Chéri'
True
您可能想了解Python和Unicode:
- 乔尔斯波尔斯基
- 内德·巴奇尔德
str()
的功能?另外,您似乎设置了sys.defaultencoding
值;这通常不是个好主意。
>>> from __future__ import unicode_literals
>>> b'Ch\xc3\xa9ri' == 'Chéri'.encode('utf8')
True
>>> b'Ch\xc3\xa9ri'.decode('utf8') == 'Chéri'
True