我正在敲桌子上关于这个错误,我已经在网上搜索但是1)无法找到答案或2)我不知道足以弄清楚发生了什么。
我的问题的总结:
当我在models.TextField中存储非ASCII字符(如è)并使用serializers.CharField序列化时,我没有遇到和出现问题。但是,如果我在模型上有一个返回相同文本字段的str方法,则在序列化时我会收到以下错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置5中的字节0xc3:序号不在范围内(128)
首先...
Python 2.7.10
。Django==1.11.7
和djangorestframework==3.7.3
。LANG
和LC_ALL
环境变量都设置为en_US.UTF-8
。PostgreSQL 9.5.6
,我验证了我的数据库是用UTF8
编码的。我的问题的细节:
我有一个简单的模型,如下所示。它有一个name
变量,它是一个TextField
以及一个简单地返回__str__
的self.name
方法。我还定义了一个testing
变量,它也简单地返回self.name
用于演示目的。
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
@python_2_unicode_compatible
class Supplier(models.Model):
name = models.TextField(help_text=_('Store Name'))
class Meta:
verbose_name = 'Supplier'
verbose_name_plural = 'Suppliers'
def testing(self):
return self.name
def __str__(self):
return self.name
接下来,我的序列化器定义如下。
class SupplierSerializer(serializers.ModelSerializer):
class Meta:
model = Supplier
fields = ('name', 'testing', '__str__')
最后,我有以下观点:
class SupplierViewSet(viewsets.ModelViewSet):
queryset = Supplier.objects.all()
serializer_class = SupplierSerializer
问题:
当我向视图发出GET
请求时,出现以下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
观察#1:
当我尝试序列化__str__
时,似乎只会出现此错误。如果我从序列化器中删除__str__
(因此序列化器只有name
和testing
)我没有得到任何错误。奇怪的是,testing
返回与__str__
相同的东西 - self.name
。
观察#2:我证实在__str__
方法中,self.name
是unicode
型。在testing
方法内部,self.name
也是unicode
类型。
观察#3:下面给出了完整的堆栈跟踪。
Traceback (most recent call last):
File "/Users//github///lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users//github///lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/Users//github///lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users//github///lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/Users//github///lib/python2.7/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/Users//github///lib/python2.7/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/Users//github///lib/python2.7/site-packages/rest_framework/renderers.py", line 105, in render
allow_nan=not self.strict, separators=separators
File "/Users//github///lib/python2.7/site-packages/rest_framework/utils/json.py", line 28, in dumps
return json.dumps(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 210, in encode
return ''.join(chunks)
这似乎是与json编码器有关的错误?
有没有更深入了解django
和unicode
的人知道发生了什么事吗?
虽然我不完全理解为什么这个修复错误的原因,但将UNICODE_JSON
设置为False
可以修复错误。
REST_FRAMEWORK = {
'UNICODE_JSON': False,
}