如何更改此代码:我不想使用原始SQL,但我不知道该怎么办

时间:2019-08-24 05:08:04

标签: python django

我不想使用SQL raw。但我不知道如何更改此代码。

查看各种材料,我试图找到一种不使用SQL raw的方法,但是失败了。我不确定该如何更改。

def listSpecificPageWork(request):    

    current_page = request.GET['current_page']

    totalCnt = DjangoBoard.objects.all().count()                  

    print 'current_page=', current_page

    boardList = DjangoBoard.objects.raw('SELECT Z.* FROM(SELECT X.*, round((rownum / %s)+0.5) as page FROM(SELECT ID,SUBJECT,NAME, CREATED_DATE, MAIL,MEMO,HITS FROM BOARD_DJANGOBOARD ORDER BY ID DESC)X)Z WHERE page = %s', [rowsPerPage, current_page])

    print  'boardList=',boardList, 'count()=', totalCnt

    pagingHelperIns = pagingHelper();

    totalPageList = pagingHelperIns.getTotalPageList( totalCnt, rowsPerPage)

    print 'totalPageList', totalPageList

    return render_to_response('listSpecificPage.html', {'boardList': boardList, 'totalCnt': totalCnt, 'current_page':int(current_page), 'totalPageList':totalPageList} )


# -*- coding: utf-8 -*-

class pagingHelper:
    "paging helper class"
    def getTotalPageList(self, total_cnt, rowsPerPage):
        if((total_cnt % rowsPerPage) == 0):
            self.total_pages = total_cnt/rowsPerPage;
            print 'getTotalPage #1'
        else:
            self.total_pages = (total_cnt/rowsPerPage) + 1;
            print 'getTotalPage #2'

        self.totalPageList = []
        for j in range(self.total_pages):
            self.totalPageList.append(j+1)

        return self.totalPageList

    def __init__(self):
        self.total_pages = 0
        self.totalPageList = 0

错误:没有这样的列:rownum

我要更改boardList。

1 个答案:

答案 0 :(得分:0)

请参阅https://docs.djangoproject.com/en/2.2/topics/db/queries/#limiting-querysets

jQuery.ajax({
type: 'GET',
async: false,
url: '/api-data',
success: function (data) {
    let trace1 = {
        x: [],
        y: [],
        mode: "lines"
    };
    let trace2 = {
        x: [],
        y: [],
        mode: "lines"
    };
    data.forEach(function(val) {
        trace1.x.push(val["time"]);
        trace1.y.push(val["pm25"]);
        trace2.x.push(val["time"]);
        trace2.y.push(val["pm10"]);
    });
    Plotly.newPlot('AQI', [trace1, trace2]);
  }
});

django还提供了https://docs.djangoproject.com/en/2.2/topics/pagination/