#导入原生sql模块from django.db import connection
classPathDetail_To_CourseAPI(View):# 展示defget(self, request):#获取路径阶段id
pd_id = request.GET.get('pd_id')#建立游标对象
cursor = connection.cursor()#执行sql语句
cursor.execute('select a.pd_id,a.course_id,b.title,b.img from pathdetail_to_course a left join course b on a.course_id = b.id where a.pd_id in (%s)'%pd_id)#获取结果
result = dictfetchall(cursor)return Response({'code':201,'data': result
})
对原生sql的结果集美化(对上面view返回的响应做出的美化)
#对原生sql结果集美化方法defdictfetchall(cursor):#获取游标描述
desc = cursor.description
return[dict(zip([col[0]for col in desc], row))for row in cursor.fetchall()]