Tastypie: JSON header to use UTF-8

老子叫甜甜 提交于 2019-12-01 06:55:03

问题


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 string data. Ex: \"Hello World\"", ...}

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


回答1:


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.

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)

You can either remove the two lines

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

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

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)

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

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


来源:https://stackoverflow.com/questions/17280513/tastypie-json-header-to-use-utf-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!