Django 序列化数据之多表数据序列化

Posted supery007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 序列化数据之多表数据序列化相关的知识,希望对你有一定的参考价值。

一、问题描述

1.将查询后的多张表的数据做序列化操作;
2.为什么要做三张表的序列化操作,因为三张表的字段有些相同有些不同;
3.将三张表序列化后的数据进行合并;
4.之后将合并后的序列化操作实现分页查询操作;

 

二、功能逻辑

data = {"code": 0, "msg": "", "result": {"data": []}}
Group1Qeryset = models.Group1.objects.all()
Group2Qeryset = models.Group2.objects.all()
Group3Qeryset = models.Group3.objects.all()

###  序列化操作
Group1Res = iam_serializer.GetListGroup1Serializer(Group1Qeryset, many=True)
Group2Res = iam_serializer.GetListGroup2Serializer(Group2Qeryset, many=True)
Group3Res = iam_serializer.GetListGroup3Serializer(Group3Qeryset, many=True)

dataAll = chain(Group1Res.data, Group2Res.data, Group3Res.data)

dataList = [dataObj for dataObj in dataAll]  ####  重新将chian数据类型转换成list类型

#### 分页功能
pageResp = pagefunc(data=dataList, currentPage=currentPage, pageSzie=pageSize)

data[result][data] = pageResp[data]
data[result][pageBean] = pageResp[pageBean]
data[msg] = 查询成功

 

三、分页功能

###  django自带的分页模块
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def pagefunc(data,currentPage,pageSzie,):
    pageSize = int(pageSzie)  # 设置每页显示条数
    dataDict = {data: [], pageBean: {}}

    # 生成分页实例
    paginator = Paginator(data, pageSize)
    # 获取数据总条数
    dataDict[pageBean][totalElements] = paginator.count
    # 每页显示条数
    dataDict[pageBean][pageSize] = pageSize
    # 总共页数
    dataDict[pageBean][totalPageNum] = paginator.num_pages

    # 获取指定页码的数据
    try:
        data = paginator.page(currentPage)
    except PageNotAnInteger:
        data = paginator.page(1)
    except EmptyPage:
        data = paginator.page(paginator.num_pages)
    # 当前多少页
    dataDict[pageBean][currentPage] = data.number

    dataDict[data] = data.object_list

    return dataDict

 

四、实际接口请求

1 请求方式:GET
2 http://127.0.0.1/xxxx/groups?currentPage=1&pageSize=2
{
    "code": 0,
    "msg": "查询成功",
    "result": {
        "data": [
            {
                "id": "f824fb1c-61cf-11ea-ab07-7247fc734bb9",
                "group_name": "xxx组",
                "users_count": 1,
                "permissions": "",
                "remark": "该组为xxx用户组,拥有所有权限",
                "create_time": "2020-03-09T00:00:00Z",
                "updata_time": "2020-03-09T00:00:00Z",
                "state": true,
                "tag": "ixxxxin",
                "type": 1
            },
            {
                "id": "20927ecd-6766-11ea-a01c-6fa3f814f5eb",
                "group_name": "xxx平台部",
                "users_count": 1,
                "permissions": "",
                "remark": "xxxx平台部",
                "create_time": "2020-03-16T09:14:24.452957Z",
                "updata_time": "2020-03-16T09:14:24.452957Z",
                "state": true,
                "tag": "",
                "type": 1
            }
        ],
        "pageBean": {
            "totalElements": 20,
            "pageSize": 2,
            "totalPageNum": 10,
            "currentPage": 1
        }
    }
}

 

以上是关于Django 序列化数据之多表数据序列化的主要内容,如果未能解决你的问题,请参考以下文章

Django之多表操作

django之多表查询与创建

八Django的orm之多表操作

django-rest-framework、多表模型继承、ModelSerializers 和嵌套序列化器

Django 之多表查询 与多表的使用

django之多表查询