django执行自定义sql语句并返回自定义Model(cursor)
个人常用:where=msg_idsql='''select t.id, t.real_name, t2.* from auth_user t join (select max(is_red) as is_red,add_user_id,task_id from card where msg_id=%s GROUP BY task_id,add_user_id)
·
个人常用:
where=msg_id
sql='''
select t.id, t.real_name, t2.* from auth_user t join (
select max(is_red) as is_red,add_user_id,task_id from card where msg_id=%s GROUP BY task_id,add_user_id)
t2 ON t2.add_user_id=t.id
''' %where
cursor = connection.cursor()
cursor.execute(sql)
fetchall = cursor.fetchall()
card=[]
for obj in fetchall:
dic={}
dic['user_id']=obj[0]
dic['real_name']=obj[1]
dic['is_red']=obj[2]
dic['add_user']=obj[3]
dic['task_id']=obj[4]
card.append(dic)
context['card']=card
节选自:https://blog.csdn.net/u012422446/article/details/52623069
更多推荐
所有评论(0)