Django:如何根据 order_id 获取 order_detail_data
Posted
技术标签:
【中文标题】Django:如何根据 order_id 获取 order_detail_data【英文标题】:Django: how to get order_detail_data as per order_id 【发布时间】:2020-06-12 15:39:20 【问题描述】: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_query)
order_result = cursor.fetchall()
order_data = list(order_result)
# print(order_data)
cursor.execute(order_detail_query)
order_detail_result = cursor.fetchall()
order_detail_data = list(order_detail_result)
# print(order_detail_data)
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)
输出:
"totalData": [
"order_data": [
"order_id": 1,
"user_id": 5,
"billing_shipping_id": 1,
"payment_method_id": 2,
"delivery_id": 2,
"txnid": "",
"order_no": "0822-1582695084-0006",
"delivery_amount": 0.0,
"discount_amount": 570.0,
"order_total": 2280.0,
"payment_status": "Unpaid",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active",
"mihpayid": "",
"payuMoneyId": ""
,
],
"order_detail_data": [
"order_detail_id": 1,
"order_id": 1,
"user_id": 5,
"product_id": 202,
"product_size_id": 867,
"size_id": 1,
"qty": 1,
"product_price": 2850.0,
"order_item_status": "Placed",
"last_status_datatime": "2020-02-26T11:01:24",
"feedback": "",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active"
,
],
,
],
我想要什么:
我希望 order_data 中的 order_detail_data 按照 order_id 如下所示:
"totalData": [
"order_data": [
"order_id": 1,
"user_id": 5,
"billing_shipping_id": 1,
"payment_method_id": 2,
"delivery_id": 2,
"txnid": "",
"order_no": "0822-1582695084-0006",
"delivery_amount": 0.0,
"discount_amount": 570.0,
"order_total": 2280.0,
"payment_status": "Unpaid",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active",
"mihpayid": "",
"payuMoneyId": "",
"order_detail_data": [
"order_detail_id": 1,
"order_id": 1,
"user_id": 5,
"product_id": 202,
"product_size_id": 867,
"size_id": 1,
"qty": 1,
"product_price": 2850.0,
"order_item_status": "Placed",
"last_status_datatime": "2020-02-26T11:01:24",
"feedback": "",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active"
],
,
"order_id": 2,
"user_id": 5,
"billing_shipping_id": 1,
"payment_method_id": 2,
"delivery_id": 2,
"txnid": "",
"order_no": "0822-1582695084-0007",
"delivery_amount": 0.0,
"discount_amount": 570.0,
"order_total": 2280.0,
"payment_status": "Unpaid",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active",
"mihpayid": "",
"payuMoneyId": "",
"order_detail_data": [
"order_detail_id": 2,
"order_id": 2,
"user_id": 5,
"product_id": 202,
"product_size_id": 867,
"size_id": 1,
"qty": 1,
"product_price": 2850.0,
"order_item_status": "Placed",
"last_status_datatime": "2020-02-26T11:01:24",
"feedback": "",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active"
], ,
# and so on...
],
,
],
"order_detail_data": [
"order_detail_id": 1,
"order_id": 1,
"user_id": 5,
"product_id": 202,
"product_size_id": 867,
"size_id": 1,
"qty": 1,
"product_price": 2850.0,
"order_item_status": "Placed",
"last_status_datatime": "2020-02-26T11:01:24",
"feedback": "",
"created_datetime": "2020-02-26T11:01:24",
"updated_datetime": "2020-02-26T05:31:24",
"status": "Active"
,
],
,
],
我使用原始查询直接从 数据库而不是 ORM。我想根据 order_data 在 order_data 中获取 order_detail_data order_id。
我正在尝试解决这个问题,但我没有得到任何可能的答案来解决这个问题。如果有人能帮助我找到我正在寻找的东西,那就太好了。非常感谢您!。
【问题讨论】:
你能证明自己任何努力解决这个问题吗? @anonymoususer 你可以再次查看你的代码,你可以自己解决,这很容易,试试这个,否则如果你不能做到,我们会在这里帮助你! 您可以将您的模型添加到帖子中吗? 我没有任何模型,因为我使用的是原始查询而不是 ORM @cabesuon 您找到解决方案了吗? @MehaParekh 【参考方案1】:如果您使用的是原始查询,那么您只需要合并数据。这样的事情应该可以工作,
def merge_order_data_and_detail(orders, details):
"""Group details by order_id and merge it in orders."""
# create dictionary key:order_id value:[order_detail_data]
dic =
for d in details:
if d['order_id'] not in dic:
dic[d['order_id']] = []
dic[d['order_id']].append(d)
# iterate orders and add details
for o in orders:
if o['order_id'] in dic:
o['order_detail_data'] = dic[o['order_id']]
orders = [
"order_id": 1,
"user_id": 5
,
"order_id": 2,
"user_id": 50
]
details = [
"order_detail_id": 1,
"order_id": 1,
"user_id": 5,
"product_id": 202
,
"order_detail_id": 2,
"order_id": 1,
"user_id": 5,
"product_id": 203
,
"order_detail_id": 3,
"order_id": 2,
"user_id": 50,
"product_id": 402
,
"order_detail_id": 4,
"order_id": 2,
"user_id": 50,
"product_id": 403
]
merge_order_data_and_detail(orders, details)
print(orders)
结果:
['order_id': 1, 'user_id': 5, 'order_detail_data': ['order_detail_id': 1, 'order_id': 1, 'user_id': 5, 'product_id': 202, 'order_detail_id': 2, 'order_id': 1, 'user_id': 5, 'product_id': 203], 'order_id': 2, 'user_id': 50, 'order_detail_data': ['order_detail_id': 3, 'order_id': 2, 'user_id': 50, 'product_id': 402, 'order_detail_id': 4, 'order_id':
2, 'user_id': 50, 'product_id': 403]]
我删除了很多订单和详细信息属性只是为了简化测试。
我没有全貌,但如果您还没有使用 Django 模型,应该考虑使用它。这是充分利用框架潜力的方式。
希望对你有帮助。
【讨论】:
你的输入数据是什么? 您在链接中提到的预期输出是答案功能的结果,您只是缺少详细信息的名称属性,没有键就不能有值字典..我在预期结果中遗漏了什么? 非常感谢。我真的很感激! @cabesuon 很高兴能帮到你!以上是关于Django:如何根据 order_id 获取 order_detail_data的主要内容,如果未能解决你的问题,请参考以下文章