原生django raw() 分页

def change_obj_to_dict(self,temp):
    dict = {}
    dict["wxh_name"] = temp.wxh_name
    dict["types"] = temp.types
    dict["subject"] = temp.subject
    dict["ids"] = temp.ids
    # 虽然产品表里没有替代型号,但是通过sql语句的raw()查询可以查到结果中是包含非实例对象的字段的
    dict["rates"] = temp.rates
    dict["pv"] = temp.pv
    dict["phone"] = temp.phone
    dict["manage_status"] = temp.manage_status
    dict["names"] = temp.names
    dict["status"] = temp.status
    return dict
models2=AutoWxh.objects.raw('SELECT auto_wxh.id, wxh_name,auto_wxh.type as types,`subject`,auto_wxh.gzh_id as ids,tab2.total_open_rate as rates,tab2.totallys_pv as pv,phone,manage_status,auto_category.name as names,auto_wxh.status as status FROM auto_wxh LEFT JOIN (SELECT gzh_id,SUM(open_rate) AS total_open_rate,SUM(total_pv) as totallys_pv FROM auto_article_list WHERE refdate="20230223" GROUP BY gzh_id) AS tab2 on auto_wxh.gzh_id = tab2.gzh_id LEFT JOIN auto_category on auto_wxh.category_id=auto_category.id ORDER BY tab2.total_open_rate DESC ')
data2 = []
if models2 and len(models2) > 0:
    for temp in models2:
        data2.append(self.change_obj_to_dict(temp))
# *****最终获得data2是一个字典的集合
page_obj = Paginator(data2, pagesize)
try:
    page_data = page_obj.page(page)
    queryset = page_data.object_list
    has_next = page_data.has_next()
except EmptyPage:
    queryset = []
    has_next = False
return SuccessResponse({'datas': queryset,'has_next': has_next,'pagenumber':page_obj.count,'pagecount':page_obj.num_pages})

思路一

主要思路就是将object对象转换成字典,再将字典装进列表中,打包进

paginator里面

django使用raw()执行sql语句多表查询_神奇洋葱头的博客-CSDN博客_django raw

思路二

if models1 and len(models1) > 0:
    for temp in models1:
        temp.__dict__.pop('_state')
        data2.append(temp.__dict__)

直接将models直接对象直接转成对象过滤多余不能识别的的键

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值