<small id='nS92U'></small><noframes id='nS92U'>

      <legend id='nS92U'><style id='nS92U'><dir id='nS92U'><q id='nS92U'></q></dir></style></legend>

        <tfoot id='nS92U'></tfoot>
        <i id='nS92U'><tr id='nS92U'><dt id='nS92U'><q id='nS92U'><span id='nS92U'><b id='nS92U'><form id='nS92U'><ins id='nS92U'></ins><ul id='nS92U'></ul><sub id='nS92U'></sub></form><legend id='nS92U'></legend><bdo id='nS92U'><pre id='nS92U'><center id='nS92U'></center></pre></bdo></b><th id='nS92U'></th></span></q></dt></tr></i><div id='nS92U'><tfoot id='nS92U'></tfoot><dl id='nS92U'><fieldset id='nS92U'></fieldset></dl></div>
          <bdo id='nS92U'></bdo><ul id='nS92U'></ul>

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

        时间:2023-08-20
        <tfoot id='fpJ3w'></tfoot>

        <small id='fpJ3w'></small><noframes id='fpJ3w'>

              <tbody id='fpJ3w'></tbody>
            • <bdo id='fpJ3w'></bdo><ul id='fpJ3w'></ul>
              • <i id='fpJ3w'><tr id='fpJ3w'><dt id='fpJ3w'><q id='fpJ3w'><span id='fpJ3w'><b id='fpJ3w'><form id='fpJ3w'><ins id='fpJ3w'></ins><ul id='fpJ3w'></ul><sub id='fpJ3w'></sub></form><legend id='fpJ3w'></legend><bdo id='fpJ3w'><pre id='fpJ3w'><center id='fpJ3w'></center></pre></bdo></b><th id='fpJ3w'></th></span></q></dt></tr></i><div id='fpJ3w'><tfoot id='fpJ3w'></tfoot><dl id='fpJ3w'><fieldset id='fpJ3w'></fieldset></dl></div>
                <legend id='fpJ3w'><style id='fpJ3w'><dir id='fpJ3w'><q id='fpJ3w'></q></dir></style></legend>

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

                  上一篇:使用 Web 服务并使用 Node.js 将 CLOB 插入 Oracle 数据 下一篇:我应该在客户端将 GraphQL ID 作为字符串处理吗?

                  相关文章

                  最新文章

                    <bdo id='03Gfz'></bdo><ul id='03Gfz'></ul>

                  <small id='03Gfz'></small><noframes id='03Gfz'>

                1. <legend id='03Gfz'><style id='03Gfz'><dir id='03Gfz'><q id='03Gfz'></q></dir></style></legend>
                  1. <i id='03Gfz'><tr id='03Gfz'><dt id='03Gfz'><q id='03Gfz'><span id='03Gfz'><b id='03Gfz'><form id='03Gfz'><ins id='03Gfz'></ins><ul id='03Gfz'></ul><sub id='03Gfz'></sub></form><legend id='03Gfz'></legend><bdo id='03Gfz'><pre id='03Gfz'><center id='03Gfz'></center></pre></bdo></b><th id='03Gfz'></th></span></q></dt></tr></i><div id='03Gfz'><tfoot id='03Gfz'></tfoot><dl id='03Gfz'><fieldset id='03Gfz'></fieldset></dl></div>
                    <tfoot id='03Gfz'></tfoot>