为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哈士奇WWW 2022-05-11 17:11:18
我不想使用原始 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。
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

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


def 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/


查看完整回答
反对 回复 2022-05-11
  • 1 回答
  • 0 关注
  • 96 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信