比较简单所以就不写注释了 自己看看吧
def api_back_descending_news(request):
# 降序
if request.POST.get('DescendingOrder'):
cursor = connection.cursor()
page = int(request.POST.get('page', 1))
limit = int(request.POST.get('limit', 10))
data = find_data(cursor,
f'select * from (select id from quick_poll_alerts order by alerts_releasetime asc limit '
f'{(page - 1) * limit},{limit}) a left join quick_poll_alerts b on a.id=b.id')
cursor.execute(f'select count(*) from quick_poll_alerts')
count = cursor.fetchall()
cursor.close()
return JsonResponse({'code': 200, 'data': data, 'count': count})
find_data
def find_data(cursor, sql):
# 格式化sql输出
cursor.execute(sql)
res = cursor.fetchall()
# label 多对多
list = []
for r in res:
data1 = {}
cursor.execute(f'select quick_poll_alerts.id, quick_poll_label.* '
f'from quick_poll_alerts, quick_poll_label, quick_poll_alerts_label '
f'where quick_poll_alerts.id=quick_poll_alerts_label.alerts_id '
f'and quick_poll_label.id=quick_poll_alerts_label.label_id '
f'and quick_poll_alerts.id={r[0]};')
list1 = cursor.fetchall()
labels = [l[2] for l in list1]
data1['id'] = r[0]
data1['img'] = r[4]
data1['labels'] = labels
data1['title'] = r[13]
data1['regional'] = Regional.objects.filter(id=r[8]).first().regional_name
data1['source'] = Source.objects.filter(id=r[11]).first().source_name
data1['total_views'] = r[10]
data1['time'] = r[12].strftime("%Y-%m-%d")
list.append(data1)
return list
网友评论