Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeDecodeError与Django Rest框架在模型&x27;s\uuuu str\uuuu字段_Python_Django_Django Rest Framework_Python Unicode - Fatal编程技术网

Python UnicodeDecodeError与Django Rest框架在模型&x27;s\uuuu str\uuuu字段

Python UnicodeDecodeError与Django Rest框架在模型&x27;s\uuuu str\uuuu字段,python,django,django-rest-framework,python-unicode,Python,Django,Django Rest Framework,Python Unicode,关于这个错误,我在桌子上使劲捶着头,我在互联网上搜索过,但不是1)找不到答案,就是2)我没有足够的知识来弄清楚到底发生了什么 我的问题的tldr摘要: 当我在models.TextField和 使用serializers.CharField序列化它,我不会遇到和 问题。但是,如果我在模型上有一个str方法 返回相同的文本字段,序列化后我得到以下结果 错误: UnicodeDecodeError:“ascii”编解码器无法解码位置中的字节0xc3 5:序号不在范围内(128) 首先… 我正在OS

关于这个错误,我在桌子上使劲捶着头,我在互联网上搜索过,但不是1)找不到答案,就是2)我没有足够的知识来弄清楚到底发生了什么

我的问题的tldr摘要:

当我在models.TextField和 使用serializers.CharField序列化它,我不会遇到和 问题。但是,如果我在模型上有一个str方法 返回相同的文本字段,序列化后我得到以下结果 错误:

UnicodeDecodeError:“ascii”编解码器无法解码位置中的字节0xc3 5:序号不在范围内(128)

首先…

  • 我正在OSX上运行
    Python2.7.10
  • 我使用的是
    Django==1.11.7
    djangorestframework==3.7.3
  • My
    LANG
    LC\u所有环境变量都设置为
    en\u US.UTF-8
  • 最后,我使用了
    postgresql9.5.6
    ,并验证了我的数据库是用
    UTF8
    编码的
我的问题的详细信息:

我有一个简单的模型,如下所示。它有一个
name
变量,该变量是
TextField
,还有一个
\uuuu str\uuuu
方法,该方法只返回
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:

只有在尝试序列化
\uuuu str\uuuu
时,才会出现此错误。如果我从序列化程序中删除
\uu str\uuu
(因此只有序列化程序
name
testing
),我不会得到任何错误。奇怪的是,
测试
返回与
\uuuu str\uuuu
-
self.name
完全相同的东西

观察#2: 我验证了在
方法中,
self.name
是type
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\u JSON
设置设置为
False
会修复错误

REST_FRAMEWORK = {
    'UNICODE_JSON': False,
}

虽然我不完全理解这会修复错误的原因,但将
UNICODE\u JSON
设置设置为
False
会修复错误

REST_FRAMEWORK = {
    'UNICODE_JSON': False,
}

除此之外,您还应该考虑升级到Python 3。为什么在串行化器上为代码< >名称> />代码>代码> >这没有道理。我认为我们需要更多地了解一下原始环境。方法需要使用@wim。为了简单起见,目前我的str方法只返回self.name。str方法实际上返回其他数据片段——我只是不想为了简单起见而显示它。但是关于你的第二点-这是一个很好的观点。。。我养成了在Meta.fields元组中放置模型字段的习惯,但是您提出了一个好的观点,即str在技术上不是字段。但这是一种方法吗?我觉得这是一种“特殊”的方法。除了别的,你应该考虑升级到Python 3。为什么在你的序列化器上同时为代码< >名称> />代码>代码>这没有道理。我认为我们需要更多地了解一下原始环境。方法需要使用@wim。为了简单起见,目前我的str方法只返回self.name。str方法实际上返回其他数据片段——我只是不想为了简单起见而显示它。但是关于你的第二点-这是一个很好的观点。。。我养成了在Meta.fields元组中放置模型字段的习惯,但是您提出了一个好的观点,即str在技术上不是字段。但这是一种方法吗?我觉得这是一种“特殊”的方法。