zoukankan      html  css  js  c++  java
  • 【Django】QuerySet的分页和排序

    数据查询分页功能和排序功能大家都很熟悉,本文以一个小例子介绍一下Django后台实现

    数据列表

    id依次从6到1

    [

    {

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

    }

    ]

     

    分页显示

    分页有两个重要的参数,一个是每页显示的记录条数,一个是页码。

    数据表查询主体代码,实现比较简单,就不解释太多,直接看代码

    稍微提醒下的两点,网上很多文章都介绍了

    1、分片代码lUserLogs[start:end],这样书写只会从数据库中获取onePageCount数据,不会获取所有数据

    2、lUserLogs.count()方式统计总数,不要用len(lUserLogs),前者是select count(*)语法,后者会返回整个查询结果集

    image

     

    分页获取第一页

    onePageCount表示单页个数,默认为20,page表示页码,默认为1

    GET http://127.0.0.1:8000/UserLog/?onePageCount=2&page=1

    -- response --

    200 OK

    Date: Sun, 22 May 2016 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", "ModifyTime": "2016-05-22 00:06:17", "IsDelete": "False", "Type": "test", "id": "5", "idUser_id": "1"}]

    分页获取第二页

    GET http://127.0.0.1:8000/UserLog/?onePageCount=2&page=2

    -- response --

    200 OK

    Date: Sun, 22 May 2016 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", "ModifyTime": "2016-05-22 00:06:16", "IsDelete": "False", "Type": "test", "id": "3", "idUser_id": "1"}]

     

    排序

    我这的数据表很多都需要排序,默认的排序方式都一样,所以提取出基类如下

    排序代码ordering = ['-ModifyTime','-CreateTime','-id']

    -符号表示逆序,从大到小,从最新的到最老的

    image

     

    实际的表继承基类即可

    image

     

    返回的数据就如同文首的数据以及本文其他的JSON返回数据结构一样,按照ordering定义的顺序排列

    好记性不如烂笔头
  • 相关阅读:
    2.采用字符的移位方式实现字符文本加密解密。
    用自己的算法实现startsWith和endsWith功能。
    采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
    输入5个数用冒泡排序进行从小到大排列
    题目六:控制台输入年龄,根据年龄输出不同的提示
    题目五:控制台输出三角形和菱形
    题目四:控制台输出九九乘法表
    百马百担
    classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();
    已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数     2008 北京奥运会男足参赛国家:  科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利
  • 原文地址:https://www.cnblogs.com/inns/p/5516539.html
Copyright © 2011-2022 走看看