Tastypie:使用UTF-8的JSON标头 [英] Tastypie: JSON header to use UTF-8

查看:139
本文介绍了Tastypie:使用UTF-8的JSON标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tastypie返回资源,其中一个字段是阿拉伯语,因此需要使用UTF-8和Unicode,这正是运行其模式的情况:

I'm using tastypie to return a Resource and one of its fields is in Arabic hence need to be in UTF-8 vs Unicode, which is what I'm assuming is the case in running its schema:

word:{...,help_text:Unicode字符串数据Ex:\Hello World \,...}

"word": {..., "help_text": "Unicode string data. Ex: \"Hello World\"", ...}

这里是json返回的示例,注意字的乱码字段:
{approved:false,id:12,resource_uri:/ api / v1 / resource / 12 /,word:اه}

Here's sample json returned, note the garbled field of word: {"approved": false, "id": 12, "resource_uri": "/api/v1/resource/12/", "word": "اه"}

推荐答案

这是因为他们修补了Tastypie不再发送charset = utf-8,当content-type为application / json或text / javascript / https://github.com / toastdriven / django-tastypie / issues / 717

This is because they patched Tastypie to no longer send charset=utf-8 when content-type is application/json or text/javascript per https://github.com/toastdriven/django-tastypie/issues/717.

如果您查看tastypie / utils / mime.py,您会注意到以下几行: p>

If you look into tastypie/utils/mime.py you will notice the following lines:

def build_content_type(format, encoding='utf-8'):
    """
    Appends character encoding to the provided format if not already present.
    """
    if 'charset' in format:
        return format

    if format in ('application/json', 'text/javascript'):
        return format

    return "%s; charset=%s" % (format, encoding)

您可以删除两行

if format in ('application/json', 'text/javascript'):
    return format

或者如果您不想修改Tastypie源代码,请执行所做的操作。

or if you don't want to modify the Tastypie source code, do what I did.

我注意到在ModelResource的create_response方法中使用了build_content_type,因此我创建了一个新的ModelResource作为ModelResource的子类,并覆盖了该方法。

I noticed that build_content_type is used in create_response method of ModelResource, so I created a new ModelResource as a subclass of ModelResource and overrode the method.

from django.http import HttpResponse
from tastypie import resources

def build_content_type(format, encoding='utf-8'):
    """
    Appends character encoding to the provided format if not already present.
    """
    if 'charset' in format:
        return format

    return "%s; charset=%s" % (format, encoding)

class MyModelResource(resources.ModelResource):
    def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
        """
        Extracts the common "which-format/serialize/return-response" cycle.

        Mostly a useful shortcut/hook.
        """
        desired_format = self.determine_format(request)
        serialized = self.serialize(request, data, desired_format)
        return response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)

然后,我将资源更改为继承自此类。 / p>

Then, I changed my resources to inherit from this class instead.

class MyResource(MyModelResource):
    class Meta:
        queryset = MyObject.objects.all()

这篇关于Tastypie:使用UTF-8的JSON标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆