标签:字段 组件 amp append for end 数据转换 python 数据
在Django的视图函数中,我们从数据库中获取数据,由以下几种方式将其序列化成JSON数据:
class PublishView(View): def get(self, request): publish_list = list(Publish.objects.all().values()) return HttpResponse(json.dumps(publish_list))
通过list强转的方式。
前台接收到的数据:
[{"id": 1, "name": "\u6e05\u534e\u5927\u5b66\u51fa\u7248\u793e", "email": "qh@gmail.com"}, {"id": 2, "name": "\u5de5\u4e1a\u51fa\u7248\u793e", "email": "gy@gmail.com"}, {"id": 3, "name": "\u90ae\u7535\u51fa\u7248\u793e", "email": "yd@gmail.com"}, {"id": 4, "name": "\u56db\u5ddd\u6587\u5b66\u51fa\u7248\u793e", "email": "scwx@gmail.com"}]
class PublishView(View): def get(self, request): publish_list = Publish.objects.all() temp = [] for obj in publish_list: temp.append({ ‘id‘: obj.id, ‘name‘: obj.name, ‘email‘: obj.email }) return HttpResponse(json.dumps(temp))
通过字段拼接的方式。
使用model_to_dict方法来将对象转换成字典:
class PublishView(View): def get(self, request): from django.forms.models import model_to_dict publish_list = Publish.objects.all() temp = [] for obj in publish_list: temp.append(model_to_dict(obj)) return HttpResponse(json.dumps(temp))
前台接收到的数据:
[{"id": 1, "name": "\u6e05\u534e\u5927\u5b66\u51fa\u7248\u793e", "email": "qh@gmail.com"}, {"id": 2, "name": "\u5de5\u4e1a\u51fa\u7248\u793e", "email": "gy@gmail.com"}, {"id": 3, "name": "\u90ae\u7535\u51fa\u7248\u793e", "email": "yd@gmail.com"}, {"id": 4, "name": "\u56db\u5ddd\u6587\u5b66\u51fa\u7248\u793e", "email": "scwx@gmail.com"}]
利用django中的序列化组件:·
class PublishView(View): def get(self, request): from django.core import serializers publish_list = Publish.objects.all() ret = serializers.serialize("json", publish_list) return HttpResponse(ret)
前台接收到的数据:
[{"model": "demo.publish", "pk": 1, "fields": {"name": "\u6e05\u534e\u5927\u5b66\u51fa\u7248\u793e", "email": "qh@gmail.com"}}, {"model": "demo.publish", "pk": 2, "fields": {"name": "\u5de5\u4e1a\u51fa\u7248\u793e", "email": "gy@gmail.com"}}, {"model": "demo.publish", "pk": 3, "fields": {"name": "\u90ae\u7535\u51fa\u7248\u793e", "email": "yd@gmail.com"}}, {"model": "demo.publish", "pk": 4, "fields": {"name": "\u56db\u5ddd\u6587\u5b66\u51fa\u7248\u793e", "email": "scwx@gmail.com"}}]
利用restframework中的serializer:
from rest_framework import serializers class PublishSerializers(serializers.Serializer): name = serializers.CharField() email = serializers.EmailField() class PublishView(View): def get(self, request): publish_list = Publish.objects.all() # 如果序列化queryset,则需要参数many=True ps = PublishSerializers(publish_list, many=True) # 如果是queryset其中一个obj,则不需要many参数 # ps = PublishSerializers(obj) return HttpResponse(ps.data)
这种方式的前提是安装djangorestframework。
前端接收到的数据:
OrderedDict([(‘name‘, ‘清华大学出版社‘), (‘email‘, ‘qh@gmail.com‘)])OrderedDict([(‘name‘, ‘工业出版社‘), (‘email‘, ‘gy@gmail.com‘)])OrderedDict([(‘name‘, ‘邮电出版社‘), (‘email‘, ‘yd@gmail.com‘)])OrderedDict([(‘name‘, ‘四川文学出版社‘), (‘email‘, ‘scwx@gmail.com‘)])
数据是多个有序字典组成的列表。
pip install djangorestframework
当我们使用postman发送POST请求时,选择form-data或x-www-form-urlencoded内容类型发送:
对应请求头中的Content-Type为application/x-www-form-urlencoded。
Django收到请求后,会将body中的数据a=1&b=2转化为request.POST字典。
<QueryDict: {‘a‘: [‘11‘], ‘b‘: [‘2‘]}> # django自动帮我们将body中的数据转换为字典
但是,如果内容类型选择raw-->Json发送:
对应请求头中的Content-Type为application/json。
Django收到请求后,不会自动将其转换为request.POST。
<QueryDict: {}> # 拿到的request.POST是空的
这时,我们通过request.body可以拿到原始的body数据:
b‘{"a":"1","b":"2"}‘
6
标签:字段 组件 amp append for end 数据转换 python 数据
原文地址:https://www.cnblogs.com/leokale-zz/p/12228597.html