码迷,mamicode.com
首页 > 其他好文 > 详细

rest-framework框架基本组件

时间:2018-09-02 02:02:14      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:elf   any   www.   字符   man   author   false   循环   组织   

一、快速实例

Quickstart

http://www.cnblogs.com/yuanchenqi/articles/8719520.html

二、序列化

1、准备模型和路由

技术分享图片
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
models.py

  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())
]

2、序列化方式一:将QuerySet对象转化为数组套字典

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

  注意:

(1)values(*field):

  调用者是queryset对象,运行后得到的并不是一系列model的实例化对象,而是一个可迭代的字典序列:

  <QuerySet [{‘name‘: ‘橘子出版社‘, ‘email‘: ‘456@qq.com‘}, {‘name‘: ‘苹果出版社‘, ‘email‘: ‘123@qq.com‘}]>

(2)list():

  将序列强行转化为数组:

  [{‘name‘: ‘橘子出版社‘, ‘email‘: ‘456@qq.com‘}, {‘name‘: ‘苹果出版社‘, ‘email‘: ‘123@qq.com‘}]

(3)json.dumps():

  json.dumps 用于将 Python 对象编码成 JSON 字符串。以下是python 原始类型向 json 类型的转化对照表:

  技术分享图片

3、序列化方式二:model_to_dict(obj)

(1)循环QuerySet构建可序列化数据结构

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

  这样写的问题是有多少字段就要加多少个字段,而且如果不知道是哪张表或者有哪些字段,就无法构建数据。

(2)引入model_to_dict完成改写

   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))

(3)测试理解model_to_dict方法

  在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‘}

  由此可见有几个字段就转化为几个键值对的字典。

4、序列化方式三:serializers.serizlize("json",publish_list)

  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

  注意:

(1)__init__.py中serialize函数原型

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).

(2)序列化后数据组织形式

"[{\"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\"}}]"

5、序列化方式四:(推荐)rest_framework  serializers

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

  注意:

(1)分析继承了Serializers的子类PublishSerializers

>>>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‘)])]

(2)Serializer是对QuerySet和model对象做序列化的

  在序列化时,第一个参数传递要序列化的对象,第二个参数many是向组件声明到底是model对象还是QuerySet。

  many=True:QuerySet      many=False:model对象

三、rest_framework(APIView)

 

rest-framework框架基本组件

标签:elf   any   www.   字符   man   author   false   循环   组织   

原文地址:https://www.cnblogs.com/xiugeng/p/9572089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!