Python:Django:如何根据 order_id 在 order_data 中获取

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

问题描述

class Order_ListAPIView(APIView):def get(self,request,format=None):总数据=[]如果 request.method == 'GET':cur,conn = 连接()order_query = ''' SELECT * FROM 订单'''order_detail_query = ''' SELECT * FROM order_details'''以 conn.cursor(MySQLdb.cursors.DictCursor) 作为游标:cursor.execute(order_detail_query)order_detail_result = cursor.fetchall()order_detail_data = 列表(order_detail_result)# 打印(order_detail_data)cursor.execute(order_query)order_result = cursor.fetchall()订单数据 = 列表(订单结果)dic = {}对于 order_detail_data 中的 d:如果 d['order_id'] 不在 dic 中:dic[d['order_id']] = []dic[d['order_id']].append(d)order_data.append(dic)totalData.append({order_data":order_data, })返回响应({totalData":totalData,},status=status.HTTP_200_OK)别的:返回响应(状态=状态.HTTP_400_BAD_REQUEST)

<块引用>

输出:

<代码>{总数据":[{订单数据":[{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,折扣金额":0.0,order_total":1.0,payment_status":已支付",created_datetime":2020-03-02T18:09:27",updated_datetime":2020-03-02T12:39:27",状态":活动",mihpayid":9956109007",payuMoneyId":306043618"},{order_id":2,用户 ID":11,billing_shipping_id":19,payment_method_id":2,delivery_id":2,txnid":",order_no":1-1583152785-0010",delivery_amount":0.0,折扣金额":0.0,order_total":1.0,payment_status":未付",created_datetime":2020-03-02T18:09:45",updated_datetime":2020-03-02T12:39:45",状态":活动",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,折扣金额":0.0,order_total":2.0,payment_status":已支付",created_datetime":2020-03-02T18:13:43",updated_datetime":2020-03-02T12:43:43",状态":活动",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,数量":1,产品价格":1.0,order_item_status":已放置",last_status_datatime":2020-03-02T18:09:27",反馈":",created_datetime":2020-03-02T18:09:27",updated_datetime":2020-03-02T12:39:27",状态":活动"}],2":[{order_detail_id":2,order_id":2,用户 ID":11,product_id":229,product_size_id":981,size_id":1,数量":1,产品价格":1.0,order_item_status":已取消",last_status_datatime":2020-03-02T18:10:19",反馈":0",created_datetime":2020-03-02T18:09:45",updated_datetime":2020-03-02T12:39:45",状态":活动"}],3":[{order_detail_id":3,order_id":3,user_id":19,product_id":229,product_size_id":982,size_id":2,数量":1,产品价格":1.0,order_item_status":已放置",last_status_datatime":2020-03-02T18:32:07",反馈":",created_datetime":2020-03-02T18:32:07",updated_datetime":2020-03-02T13:02:07",状态":活动"},]}]}]}

<块引用>

我想要的是:

<代码>{总数据":[{订单数据":[{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,折扣金额":0.0,order_total":1.0,payment_status":已支付",created_datetime":2020-03-02T18:09:27",updated_datetime":2020-03-02T12:39:27",状态":活动",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,数量":1,产品价格":1.0,order_item_status":已放置",last_status_datatime":2020-03-02T18:09:27",反馈":",created_datetime":2020-03-02T18:09:27",updated_datetime":2020-03-02T12:39:27",状态":活动"],}},{order_id":2,用户 ID":11,billing_shipping_id":19,payment_method_id":2,delivery_id":2,txnid":",order_no":1-1583152785-0010",delivery_amount":0.0,折扣金额":0.0,order_total":1.0,payment_status":未付",created_datetime":2020-03-02T18:09:45",updated_datetime":2020-03-02T12:39:45",状态":活动",mihpayid":",payuMoneyId":",2":[{order_detail_id":2,order_id":2,用户 ID":11,product_id":229,product_size_id":981,size_id":1,数量":1,产品价格":1.0,order_item_status":已取消",last_status_datatime":2020-03-02T18:10:19",反馈":0",created_datetime":2020-03-02T18:09:45",updated_datetime":2020-03-02T12:39:45",状态":活动"],}},{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,折扣金额":0.0,order_total":2.0,payment_status":已支付",created_datetime":2020-03-02T18:13:43",updated_datetime":2020-03-02T12:43:43",状态":活动",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,数量":1,产品价格":1.0,order_item_status":已放置",last_status_datatime":2020-03-02T18:32:07",反馈":",created_datetime":2020-03-02T18:32:07",updated_datetime":2020-03-02T13:02:07",状态":活动"]},]}]}

<块引用>

我使用原始查询从数据库中直接获取数据而不是ORM.我想按照 order_data 在 order_data 中获取 order_detail_dataorder_id,根据上述详细信息.

我正在尝试解决这个问题,但我没有得到任何可以解决这个问题的可能答案.如果有人能帮我解决我正在寻找的问题,那就太好了.非常感谢您!

解决方案

经过 16 个小时的努力,我找到了解决方案:

class Order_ListAPIView(APIView):def get(self,request,format=None):总数据=[]如果 request.method == 'GET':cur,conn = 连接()order_query = ''' SELECT * FROM 订单'''order_detail_query = ''' SELECT * FROM order_details LEFT JOIN 订单 ON order_details.order_detail_id = orders.order_id '''以 conn.cursor(MySQLdb.cursors.DictCursor) 作为游标:cursor.execute(order_detail_query)order_detail_result = cursor.fetchall()order_detail_data = 列表(order_detail_result)# 打印(order_detail_data)cursor.execute(order_query)order_result = cursor.fetchall()订单数据 = 列表(订单结果)dic = {}def merge_order_data_and_detail(order_data, order_detail_data):对于 order_detail_data 中的 d:如果 d['order_id'] 不在 dic 中:dic[d['order_id']] = []dic[d['order_id']].append(d)对于 o 在 order_data 中:如果 o['order_id'] 在 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, })返回响应({"totalData":totalData,},status=status.HTTP_200_OK)别的:返回响应(状态=状态.HTTP_400_BAD_REQUEST)

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)

output:

{
    "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"
                        },
                    ]
                }
            ]
        }
    ]
}

what i want is:

{
    "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"
                    ]
                },
            ]
        }
    ]
}

i am direct fetching data using raw query from databases instead of ORM. i want to get order_detail_data inside order_data as per order_id, per above details.

i am trying to solve this problem, but i didn't get any possible answer which could solve this problem. It would be great if anyone could help me out for what i am looking for. Advance thank you so much!.

解决方案

After 16 hours struggle i found solution:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)