简体   繁体   中英

TypeError:string indices must be integers

i am getting following error- "Python: TypeError:string indices must be integers" and I can not see what's wrong. Am I being stupid and overlooking an obvious mistake here?

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)

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)

This should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM