How to turn a list into a paginated JSON response for REST?

柔情痞子 提交于 2021-01-28 06:58:41

问题


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

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?

What client is waiting for is:

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

The question is: How to paginate such response?

Any input is highly appreciated!

Best regards, Alexey.


回答1:


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 imports the json package, which is a JSON serialization/deserialization library

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

[{"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

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

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

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.

EDIT: Pagination without storing pages

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


来源:https://stackoverflow.com/questions/44635903/how-to-turn-a-list-into-a-paginated-json-response-for-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!