简体   繁体   English

类型错误:字符串索引必须是整数

[英]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.我收到以下错误 - “Python:TypeError:字符串索引必须是整数”,我看不出有什么问题。 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这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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