如何将列表转换为REST的分页JSON响应? [英] How to turn a list into a paginated JSON response for REST?

查看:29
本文介绍了如何将列表转换为REST的分页JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django REST Framework的新手,我遇到了一个问题.

I'm new to Django REST Framework and I faced a problem.

我正在为社交应用构建后端.该任务是将分页的JSON响应返回给客户端.在文档中,我只找到了如何对模型实例执行此操作,但是我只有一个列表:

I'm building a backend for a social app. The task is to return a paginated JSON response to client. In the docs I only found how to do that for model instances, but what I have is a list:

[368625, 507694, 687854, 765213, 778491, 1004752, 1024781, 1303354, 1311339, 1407238, 1506842, 1530012, 1797981, 2113318, 2179297, 2312363, 2361973, 2610241, 3005224, 3252169, 3291575, 3333882, 3486264, 3860625, 3964299, 3968863, 4299124, 4907284, 4941503, 5120504, 5210060, 5292840, 5460981, 5622576, 5746708, 5757967, 5968243, 6025451, 6040799, 6267952, 6282564, 6603517, 7271663, 7288106, 7486229, 7600623, 7981711, 8106982, 8460028, 10471602]

有一些不错的方法吗?我必须以某种特殊方式对其进行序列化吗?

Is there some nice way to do it? Do I have to serialize it in some special way?

客户端正在等待的是:

{"users": [{"id": "368625"}, {"id": "507694"}, ...]}

问题是:如何对这种回应进行分页?

The question is: How to paginate such response?

任何输入都将受到高度赞赏!

Any input is highly appreciated!

最诚挚的问候,阿列克谢.

Best regards, Alexey.

推荐答案

TLDR:

import json
data=[368625, 507694, 687854, 765213, 778491, 1004752, 1024781, 1303354, 1311339, 1407238, 1506842, 1530012, 1797981, 2113318, 2179297, 2312363, 2361973, 2610241, 3005224, 3252169, 3291575, 3333882, 3486264, 3860625, 3964299, 3968863, 4299124, 4907284, 4941503, 5120504, 5210060, 5292840, 5460981, 5622576, 5746708, 5757967, 5968243, 6025451, 6040799, 6267952, 6282564, 6603517, 7271663, 7288106, 7486229, 7600623, 7981711, 8106982, 8460028, 10471602]
print(json.dumps({"users":[{"id":value} for value in data]}))

import json 导入json包,这是一个JSON序列化/反序列化库

import json imports the json package, which is a JSON serialization/deserialization library

json.dumps(obj)使用python对象 obj 并将其序列化为JSON字符串

json.dumps(obj) takes obj, a python object, and serializes it to a JSON string

[[{"id":value}表示数据中的值] 只是一个列表推导,它创建一个Python字典列表,其中"id" 映射到每个值在 data 数组

[{"id":value} for value in data] is just a list comprehension which creates a list of python dictionaries with "id" mapped to each value in the data array

分页我不确定分页是否有一些标准,但是一个简单的模型将是:

Pagination I'm not sure if there's some standard on pagination, but a simple model would be:

"data": {
    "prevPage": "id",
    "nextPage": "id",
    "data": [
       ...
    ]
}

老实说,在python中实现它并不难:

Honestly, implementing that in python wouldn't be that hard:

data=[ ... ]
currentPage={"pageID":0,"data":[]}
prevPage={"pageID":-1}

pageSize=5

for value in data:
    currentPage["data"].append({"id":value})
    if len(currentPage)==pageSize:
        currentPage["prevPage"]=prevPage["pageID"]
        prevPage["nextPage"]=currentPage["pageID"]
        # add currentPage to some database of pages
        prevPage=currentPage
        currentPage={"pageID":"generate new page id","data":[]}

显然,这不是很好,但是显示了基本概念.

Obviously, this isn't very polished, but shows the basic concept.

分页而不存储页面

您当然可以在每次请求时重新创建页面:

You could of course recreate the page every time it is requested:

def getPage(pageNum)
    #calculate pageStart and pageEnd based on your own requiremnets
    pageStart = (pageNum // 5) * 5
    pageEnd = (pageNum // 5)*5+5
    return [{"id":data[idx] for idx in range(pageStart, pageEnd)}]

这篇关于如何将列表转换为REST的分页JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆