标签:elf any www. 字符 man author false 循环 组织
http://www.cnblogs.com/yuanchenqi/articles/8719520.html
from django.db import models # Create your models here. class Book(models.Model): title = models.CharField(max_length=32) price = models.IntegerField() pub_date = models.DateField() publish = models.ForeignKey("Publish", on_delete=models.CASCADE) authors = models.ManyToManyField("Author") def __str__(self): return self.title class Publish(models.Model): name = models.CharField(max_length=32) email = models.EmailField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() def __str__(self): return self.name
urls.py:
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘publishes/‘, views.PublishView.as_view()) ]
from django.shortcuts import render, HttpResponse from django.views import View from .models import Publish import json class PublishView(View): def get(self, request): # QuerySet对象不能进行json序列化 # 方式1:values(*field):调用者是queryset对象,运行后得到的并不是一系列model的实例化对象,而是一个可迭代的字典序列 # 再使用list()方法强转为列表,组成列表里面放字典的数据结构 publish_list = list(Publish.objects.all().values("name", "email")) return HttpResponse(json.dumps(publish_list)) def post(self, request): pass
注意:
调用者是queryset对象,运行后得到的并不是一系列model的实例化对象,而是一个可迭代的字典序列:
<QuerySet [{‘name‘: ‘橘子出版社‘, ‘email‘: ‘456@qq.com‘}, {‘name‘: ‘苹果出版社‘, ‘email‘: ‘123@qq.com‘}]>
将序列强行转化为数组:
[{‘name‘: ‘橘子出版社‘, ‘email‘: ‘456@qq.com‘}, {‘name‘: ‘苹果出版社‘, ‘email‘: ‘123@qq.com‘}]
json.dumps 用于将 Python 对象编码成 JSON 字符串。以下是python 原始类型向 json 类型的转化对照表:
from django.views import View from .models import Publish import json class PublishView(View): def get(self, request): # QuerySet对象不能进行json序列化 # 方式2: publish_list = Publish.objects.all() temp = [] for obj in publish_list: temp.append({ "name": obj.name, "name": obj.email }) print(temp) # [{‘name‘: ‘456@qq.com‘}, {‘name‘: ‘123@qq.com‘}] return HttpResponse(json.dumps(temp)) def post(self, request): pass
这样写的问题是有多少字段就要加多少个字段,而且如果不知道是哪张表或者有哪些字段,就无法构建数据。
model_to_dict是用于将model对象转换为字典的方法。
from django.views import View from .models import Publish import json class PublishView(View): def get(self, request): # QuerySet对象不能进行json序列化 # 方式2: 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)) print(temp) # [{‘name‘: ‘456@qq.com‘}, {‘name‘: ‘123@qq.com‘}] return HttpResponse(json.dumps(temp))
在pycharm的python console测试:
>>>from django.forms.models import model_to_dict >>>from app01 import models >>>print(models) <module ‘app01.models‘ from ‘/Users/hqs/PycharmProjects/restDemo/app01/models.py‘> >>>obj = models.Publish.objects.all() >>>print(obj) <QuerySet [<Publish: 橘子出版社>, <Publish: 苹果出版社>]> >>>obj = models.Publish.objects.filter(pk=2).first() >>>obj # obj是一个model对象 <Publish: 橘子出版社> >>>model_to_dict(obj) {‘id‘: 2, ‘name‘: ‘橘子出版社‘, ‘email‘: ‘456@qq.com‘}
由此可见有几个字段就转化为几个键值对的字典。
serializers是django的序列化组件。
from django.views import View from .models import Publish import json class PublishView(View): def get(self, request): # QuerySet对象不能进行json序列化 # 方式3: from django.core import serializers publish_list = Publish.objects.all() ret = serializers.serialize("json", publish_list) return HttpResponse(json.dumps(ret)) def post(self, request): pass
注意:
def serialize(format, queryset, **options): """ Serialize a queryset (or any iterator that returns database objects) using a certain serializer. """ s = get_serializer(format)() s.serialize(queryset, **options) return s.getvalue()
传递给 serialize 方法的参数有二:一个序列化目标格式,另外一个是序列化的对象QuerySet. (事实上,第二个参数可以是任何可迭代的Django Model实例,但它很多情况下就是一个QuerySet).
"[{\"model\": \"app01.publish\", \"pk\": 2, \"fields\": {\"name\": \"\\u6a58\\u5b50\\u51fa\\u7248\\u793e\", \"email\": \"456@qq.com\"}}, {\"model\": \"app01.publish\", \"pk\": 3, \"fields\": {\"name\": \"\\u82f9\\u679c\\u51fa\\u7248\\u793e\", \"email\": \"123@qq.com\"}}]"
from django.views import View from .models import Publish import json from rest_framework import serializers class PublishSerializers(serializers.Serializer): """为QuerySet做序列化""" name = serializers.CharField() email = serializers.CharField() class PublishView(View): def get(self, request): # 方式4: publish_list = Publish.objects.all() ret = PublishSerializers(publish_list, many=True) # 描述是model对象还是QuerySet True:queryset return HttpResponse(json.dumps(ret)) def post(self, request): pass
注意:
>>>from app01.views import PublishSerializers >>>publish_list = models.Publish.objects.all() >>>PublishSerializers(publish_list, many=True) # 描述是model对象还是QuerySet PublishSerializers(<QuerySet [<Publish: 橘子出版社>, <Publish: 苹果出版社>]>, many=True): name = CharField() email = CharField() >>>ps = PublishSerializers(publish_list, many=True) >>>ps.data [OrderedDict([(‘name‘, ‘橘子出版社‘), (‘email‘, ‘456@qq.com‘)]), OrderedDict([(‘name‘, ‘苹果出版社‘), (‘email‘, ‘123@qq.com‘)])]
在序列化时,第一个参数传递要序列化的对象,第二个参数many是向组件声明到底是model对象还是QuerySet。
many=True:QuerySet many=False:model对象
标签:elf any www. 字符 man author false 循环 组织
原文地址:https://www.cnblogs.com/xiugeng/p/9572089.html