本文介绍了Python:Django:如何根据order_id在order_data中获取order_detail_data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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_data:
                    if d['order_id'] not in dic:
                        dic[d['order_id']] = []
                    dic[d['order_id']].append(d)
                order_data.append(dic)
 
            totalData.append({"order_data":order_data, })
            return Response({"totalData":totalData,},status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)
{
    "totalData": [
        {
            "order_data": [
                {
                    "order_id": 1,
                    "user_id": 4,
                    "billing_shipping_id": 5,
                    "payment_method_id": 1,
                    "delivery_id": 2,
                    "txnid": "584ffb7fd622eca10a6d",
                    "order_no": "1-1583152683-0005",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 1.0,
                    "payment_status": "Paid",
                    "created_datetime": "2020-03-02T18:09:27",
                    "updated_datetime": "2020-03-02T12:39:27",
                    "status": "Active",
                    "mihpayid": "9956109007",
                    "payuMoneyId": "306043618"
                },
                {
                    "order_id": 2,
                    "user_id": 11,
                    "billing_shipping_id": 19,
                    "payment_method_id": 2,
                    "delivery_id": 2,
                    "txnid": "",
                    "order_no": "1-1583152785-0010",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 1.0,
                    "payment_status": "Unpaid",
                    "created_datetime": "2020-03-02T18:09:45",
                    "updated_datetime": "2020-03-02T12:39:45",
                    "status": "Active",
                    "mihpayid": "",
                    "payuMoneyId": ""
                },
                {
                    "order_id": 3,
                    "user_id": 12,
                    "billing_shipping_id": 20,
                    "payment_method_id": 1,
                    "delivery_id": 2,
                    "txnid": "83e066ca75437f3d05b0",
                    "order_no": "2-1583152964-0019",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 2.0,
                    "payment_status": "Paid",
                    "created_datetime": "2020-03-02T18:13:43",
                    "updated_datetime": "2020-03-02T12:43:43",
                    "status": "Active",
                    "mihpayid": "9956136979",
                    "payuMoneyId": "306044826"
                },
                {
                    "1": [
                        {
                            "order_detail_id": 1,
                            "order_id": 1,
                            "user_id": 4,
                            "product_id": 229,
                            "product_size_id": 982,
                            "size_id": 2,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Placed",
                            "last_status_datatime": "2020-03-02T18:09:27",
                            "feedback": "",
                            "created_datetime": "2020-03-02T18:09:27",
                            "updated_datetime": "2020-03-02T12:39:27",
                            "status": "Active"
                        }
                    ],
                    "2": [
                        {
                            "order_detail_id": 2,
                            "order_id": 2,
                            "user_id": 11,
                            "product_id": 229,
                            "product_size_id": 981,
                            "size_id": 1,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Cancelled",
                            "last_status_datatime": "2020-03-02T18:10:19",
                            "feedback": "0",
                            "created_datetime": "2020-03-02T18:09:45",
                            "updated_datetime": "2020-03-02T12:39:45",
                            "status": "Active"
                        }
                    ],
                    "3": [
                        {
                            "order_detail_id": 3,
                            "order_id": 3,
                            "user_id": 19,
                            "product_id": 229,
                            "product_size_id": 982,
                            "size_id": 2,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Placed",
                            "last_status_datatime": "2020-03-02T18:32:07",
                            "feedback": "",
                            "created_datetime": "2020-03-02T18:32:07",
                            "updated_datetime": "2020-03-02T13:02:07",
                            "status": "Active"
                        },
                    ]
                }
            ]
        }
    ]
}
{
    "totalData": [
        {
            "order_data": [
                {
                    "order_id": 1,
                    "user_id": 4,
                    "billing_shipping_id": 5,
                    "payment_method_id": 1,
                    "delivery_id": 2,
                    "txnid": "584ffb7fd622eca10a6d",
                    "order_no": "1-1583152683-0005",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 1.0,
                    "payment_status": "Paid",
                    "created_datetime": "2020-03-02T18:09:27",
                    "updated_datetime": "2020-03-02T12:39:27",
                    "status": "Active",
                    "mihpayid": "9956109007",
                    "payuMoneyId": "306043618",
                    "1": [
                        {
                            "order_detail_id": 1,
                            "order_id": 1,
                            "user_id": 4,
                            "product_id": 229,
                            "product_size_id": 982,
                            "size_id": 2,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Placed",
                            "last_status_datatime": "2020-03-02T18:09:27",
                            "feedback": "",
                            "created_datetime": "2020-03-02T18:09:27",
                            "updated_datetime": "2020-03-02T12:39:27",
                            "status": "Active" 
                    ],}
                },
                {
                    "order_id": 2,
                    "user_id": 11,
                    "billing_shipping_id": 19,
                    "payment_method_id": 2,
                    "delivery_id": 2,
                    "txnid": "",
                    "order_no": "1-1583152785-0010",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 1.0,
                    "payment_status": "Unpaid",
                    "created_datetime": "2020-03-02T18:09:45",
                    "updated_datetime": "2020-03-02T12:39:45",
                    "status": "Active",
                    "mihpayid": "",
                    "payuMoneyId": "",
                    "2": [
                        {
                            "order_detail_id": 2,
                            "order_id": 2,
                            "user_id": 11,
                            "product_id": 229,
                            "product_size_id": 981,
                            "size_id": 1,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Cancelled",
                            "last_status_datatime": "2020-03-02T18:10:19",
                            "feedback": "0",
                            "created_datetime": "2020-03-02T18:09:45",
                            "updated_datetime": "2020-03-02T12:39:45",
                            "status": "Active" 
                    ],
                   
                }
                },
                {
                    "order_id": 3,
                    "user_id": 12,
                    "billing_shipping_id": 20,
                    "payment_method_id": 1,
                    "delivery_id": 2,
                    "txnid": "83e066ca75437f3d05b0",
                    "order_no": "2-1583152964-0019",
                    "delivery_amount": 0.0,
                    "discount_amount": 0.0,
                    "order_total": 2.0,
                    "payment_status": "Paid",
                    "created_datetime": "2020-03-02T18:13:43",
                    "updated_datetime": "2020-03-02T12:43:43",
                    "status": "Active",
                    "mihpayid": "9956136979",
                    "payuMoneyId": "306044826",
                    "3": [
                        {
                            "order_detail_id": 3,
                            "order_id": 3,
                            "user_id": 19,
                            "product_id": 229,
                            "product_size_id": 982,
                            "size_id": 2,
                            "qty": 1,
                            "product_price": 1.0,
                            "order_item_status": "Placed",
                            "last_status_datatime": "2020-03-02T18:32:07",
                            "feedback": "",
                            "created_datetime": "2020-03-02T18:32:07",
                            "updated_datetime": "2020-03-02T13:02:07",
                            "status": "Active"
                    ]
                },
            ]
        }
    ]
}

我正在尝试解决此问题,但没有得到任何可能的答案来解决此问题.如果有人可以帮助我寻找我想要的东西,那就太好了.提前非常感谢您!

推荐答案

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 LEFT JOIN orders ON order_details.order_detail_id = orders.order_id '''

            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 = {}
                def merge_order_data_and_detail(order_data, order_detail_data):
                    for d in order_detail_data:
                        if d['order_id'] not in dic:
                            dic[d['order_id']] = []
                        dic[d['order_id']].append(d)


                    for o in order_data:
                        if o['order_id'] in dic:
                            o['order_detail_data'] = dic[o['order_id']]

                merge_order_data_and_detail(order_data, order_detail_data)

            totalData.append({"order_data":order_data, })
            return Response({"totalData":totalData,},status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)


这篇关于Python:Django:如何根据order_id在order_data中获取order_detail_data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 05:07