Data query paging function and sorting function everyone is familiar with this article with a small example of the Django background implementation
Data list
ID from 6 to 1 in turn
[
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:36",
"Modifytime": "2016-05-22 00:06:36",
"Isdelete": "False",
"Type": "Test",
"id": "6",
"iduser_id": "1"
},
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:17",
"Modifytime": "2016-05-22 00:06:17",
"Isdelete": "False",
"Type": "Test",
"id": "5",
"iduser_id": "1"
},
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:17",
"Modifytime": "2016-05-22 00:06:17",
"Isdelete": "False",
"Type": "Test",
"id": "4",
"iduser_id": "1"
},
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:16",
"Modifytime": "2016-05-22 00:06:16",
"Isdelete": "False",
"Type": "Test",
"id": "3",
"iduser_id": "1"
},
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:15",
"Modifytime": "2016-05-22 00:06:15",
"Isdelete": "False",
"Type": "Test",
"id": "2",
"iduser_id": "1"
},
{
"Detail": "This is Test",
"Createtime": "2016-05-22 00:06:12",
"Modifytime": "2016-05-22 00:06:12",
"Isdelete": "False",
"Type": "Test",
"id": "1",
"iduser_id": "1"
}
]
Pagination Display
Paging has two important parameters, one is the number of records displayed per page, and one is the page number.
Data Table Query Body code, implementation is relatively simple, do not explain too much, look directly at the code
A little reminder of the next two points, many articles on the internet have introduced
1, Shard code Luserlogs[start:end], so that writing will only get Onepagecount data from the database, will not get all the data
2, Luserlogs.count () statistics total, do not use Len (luserlogs), the former is the Select COUNT (*) syntax, which will return the entire query result set
Pagination get first page
Onepagecount represents the number of pages, the default is 20,page for page numbers, the default is 1
GET http://127.0.0.1:8000/UserLog/?onePageCount=2&page=1
--Response--
OK
Date:sun, 04:07:04 GMT
server:wsgiserver/0.1 python/2.7.10
Vary:cookie
X-frame-options:sameorigin
Content-type:application/json
Set-cookie:csrftoken=ma0qffh87zllpjqt0blupb16f7waoih8; Expires=sun, 21-may-2017 04:07:04 GMT; max-age=31449600; path=/
[{"Detail": "This is Test", "Createtime": "2016-05-22 00:06:36", "Modifytime": "2016-05-22 00:06:36", "Isdelete": "False" , "Type": "Test", "id": "6", "iduser_id": "1"}, {"Detail": "This is Test", "Createtime": "2016-05-22 00:06:17", "Modifytim E ":" 2016-05-22 00:06:17 "," Isdelete ":" False "," Type ":" Test "," id ":" 5 "," iduser_id ":" 1 "}] pagination get second page
GET http://127.0.0.1:8000/UserLog/?onePageCount=2&page=2
--Response--
OK
Date:sun, 04:11:07 GMT
server:wsgiserver/0.1 python/2.7.10
Vary:cookie
X-frame-options:sameorigin
Content-type:application/json
Set-cookie:csrftoken=ma0qffh87zllpjqt0blupb16f7waoih8; Expires=sun, 21-may-2017 04:11:07 GMT; max-age=31449600; path=/
[{"Detail": "This is Test", "Createtime": "2016-05-22 00:06:17", "Modifytime": "2016-05-22 00:06:17", "Isdelete": "False" , "Type": "Test", "id": "4", "iduser_id": "1"}, {"Detail": "This is Test", "Createtime": "2016-05-22 00:06:16", "Modifytim E ":" 2016-05-22 00:06:16 "," Isdelete ":" False "," Type ":" Test "," id ":" 3 "," iduser_id ":" 1 "}]
Sort
Many of my data tables need to be sorted, the default is the same way, so extract the base class as follows
Sort code ordering = ['-modifytime ', '-createtime ', '-id ']
-Symbols denote reverse order, from big to small, from newest to oldest
The actual table inherits the base class to
The returned data is just like the first data and the other JSON return data structures in this article, sorted in the order defined by ordering
"Django" queryset pagination and sorting