我遇到以下错误-“ Python:TypeError:字符串索引必须为整数”,我看不到有什么问题。我是不是很愚蠢,在这里忽略了一个明显的错误?
class Order_ListAPIView(APIView):
def get(self,request,format=None):
totalData=[]
if request.method == 'GET':
cur,conn = connection()
order_query = ''' SELECT * FROM orders'''
order_detail_query = ''' SELECT * FROM order_details'''
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(order_detail_query)
order_detail_result = cursor.fetchall()
order_detail_data = list(order_detail_result)
# print(order_detail_data)
cursor.execute(order_query)
order_result = cursor.fetchall()
order_data = list(order_result)
dic = {}
for d in order_detail_query:
if d['order_id'] not in dic:
dic[d['order_id']] = []
dic[d['order_id']].append(d)
return order_data.append(dic)
totalData.append({"order_data":order_data, "order_detail_data":order_detail_data})
return Response({"totalData":totalData,},status=status.HTTP_200_OK)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
答案 0 :(得分:0)
lass Order_ListAPIView(APIView):
def get(self,request,format=None):
totalData=[]
if request.method == 'GET':
cur,conn = connection()
order_query = ''' SELECT * FROM orders'''
order_detail_query = ''' SELECT * FROM order_details'''
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
cursor.execute(order_detail_query)
order_detail_result = cursor.fetchall()
order_detail_data = list(order_detail_result)
# print(order_detail_data)
cursor.execute(order_query)
order_result = cursor.fetchall()
order_data = list(order_result)
dic = {}
for d in order_detail_data:
if d['order_id'] not in dic:
dic[d['order_id']] = []
dic[d['order_id']].append(d)
return order_data.append(dic)
totalData.append({"order_data":order_data, "order_detail_data":order_detail_data})
return Response({"totalData":totalData,},status=status.HTTP_200_OK)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
这应该有效