我创建一个表成员,其中包含一个需要存储日语的字段“名称”。我使用Web服务访问表数据。下面是代码,

from django.http import HttpResponse
from rest_framework.renderers import JSONRenderer

class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders it's content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

from rest_framework import serializers
class MemberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Member
        fields = ('member_id', 'name', 'homepage', 'map')

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new Member instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.member_id = attrs.get('member_id', instance.member_id)
            instance.name = attrs.get('name', instance.name)
            instance.homepage = attrs.get('homepage', instance.homepage)
            instance.map = attrs.get('map', instance.map)
            return instance

        # Create new instance
        return Member(**attrs)

//snapshot code
serializer = MemberSerializer(member)
return JSONResponse(serializer.data)


'name'值显示为unicode,{“ name”:“ \ u691c \ u8a3a \ u3092 \ u4e88 \ u7d04 \ u3059 \ u308b”}。如何将其转换为日语?

最佳答案

尝试UnicodeJSONRendererJSONRenderer使用\u语法转义非ASCII字符; UnicodeJSONRenderer不会那样做。

关于python - Django Unicode转换为日语,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18070331/

10-11 17:45