gpt4 book ai didi

python - django unicode 转换为日语

转载 作者:行者123 更新时间:2023-11-30 23:34:29 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com