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

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


看了各种资料,想办法不使用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。


哈士奇WWW
浏览 97回答 1
1回答

猛跑小猪

见https://docs.djangoproject.com/en/2.2/topics/db/queries/#limiting-querysetsdef get_page(page_num,per_page=100):    offset = page_num*per_page    return  DjangoDashboard.objects.filter(...).all()[offset:offset+per_page]get_page(2) # gets the 3rd page (0 based...)django 还提供了https://docs.djangoproject.com/en/2.2/topics/pagination/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python