补充通用分页函数的注释和用法;修改错误的测试用例
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
|
|
||||||
from rest_framework import pagination
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
|
||||||
@@ -20,6 +19,31 @@ def success_response(data):
|
|||||||
def paginate(request, query_set, object_serializer):
|
def paginate(request, query_set, object_serializer):
|
||||||
"""
|
"""
|
||||||
用于分页的函数
|
用于分页的函数
|
||||||
|
如果 url 里面不含有paging=true,那么将返回全部数据。类似
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"username": "1111111",
|
||||||
|
"password": "123456"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
如果 url 中有 paging=true 的参数,
|
||||||
|
然后还需要读取其余的两个参数,page=[int],需要的页码,p
|
||||||
|
age_size=[int],一页的数据条数
|
||||||
|
参数错误的时候,返回{"code": 1, "data": u"参数错误"}
|
||||||
|
返回的数据格式
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"data": {
|
||||||
|
"previous_page": null,
|
||||||
|
"results": [
|
||||||
|
{
|
||||||
|
"username": "1111111",
|
||||||
|
"password": "123456"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"next_page": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
:param query_set 数据库查询结果
|
:param query_set 数据库查询结果
|
||||||
:param object_serializer: 序列化单个object的serializer
|
:param object_serializer: 序列化单个object的serializer
|
||||||
:return response
|
:return response
|
||||||
|
|||||||
@@ -35,19 +35,19 @@ class PaginatorTest(APITestCase):
|
|||||||
response = self.client.get(self.url + "?paging=true")
|
response = self.client.get(self.url + "?paging=true")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
response = self.client.get(self.url + "?paging=true&limit=-1")
|
response = self.client.get(self.url + "?paging=true&page_size=-1")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
response = self.client.get(self.url + "?paging=true&limit=aa")
|
response = self.client.get(self.url + "?paging=true&page_size=aa")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
response = self.client.get(self.url + "?paging=true&limit=1&page_size=1&page=-1")
|
response = self.client.get(self.url + "?paging=true&page_size=1&page=-1")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
response = self.client.get(self.url + "?paging=true&limit=1&page_size=aaa&page=1")
|
response = self.client.get(self.url + "?paging=true&page_size=aaa&page=1")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
response = self.client.get(self.url + "?paging=true&limit=1&page_size=1&page=aaa")
|
response = self.client.get(self.url + "?paging=true&page_size=1&page=aaa")
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
|
||||||
|
|
||||||
def test_correct_paginate(self):
|
def test_correct_paginate(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user