我的queryset输出是
[{'ACCOUNT_NAME': 'MOHAMMAD FAWAD KHALID',
'ACCOUNT_SNO': 1810028081,
'ACTIVETRACKING': 1,
'CAMPAIGN_CODE': 'Testing',
'CAMPAIGN_DESCRIPTION': 'First Testing Campaign',
'CAMPAIGN_DOCS_ID': 121,
'CAMPAIGN_OBJECTIVE_ID': 2,
'CAMP_DETAIL_ID': 1462,
'CAMP_END': datetime.datetime(2020, 2, 1, 0, 0),
'CAMP_START': datetime.datetime(2020, 1, 1, 0, 0),
'CUSTOMER_EMAIL': 'm.fawadkhalid@gmail.com',
'DOCUMENT': 'App_download_urdu_1.html',
'ID': 61,
'ISACTIVE': 1,
'LAST_CYCLE': '2',
'MAILSTATUS_APP': 'D',
'MAILSUBJECT': 'MCBAH Testing Campaign',
'MOBILE_NO': '923000704342',
'OBJECTIVE': 'SIP Payment',
'TRACKINGCYCLE': 5}]
我需要按如下方式转换上面的列表:
[{'DATA': {'ACCOUNT_NAME': 'MOHAMMAD FAWAD KHALID',
'ACCOUNT_SNO': 1810028081,
'ACTIVETRACKING': 1,
'CAMPAIGN_CODE': 'Testing',
'CAMPAIGN_DESCRIPTION': 'First Testing Campaign',
'CAMPAIGN_DOCS_ID': 121,
'CAMPAIGN_OBJECTIVE_ID': 2,
'CAMP_DETAIL_ID': 1462,
'CAMP_END': datetime.datetime(2020, 2, 1, 0, 0),
'CAMP_START': datetime.datetime(2020, 1, 1, 0, 0),
'CUSTOMER_EMAIL': 'm.fawadkhalid@gmail.com',
'DOCUMENT': 'App_download_urdu_1.html',
'ISACTIVE': 1,
'LAST_CYCLE': '2',
'MAILSTATUS_APP': 'D',
'MAILSUBJECT': 'MCBAH Testing Campaign',
'MOBILE_NO': '923000704342',
'OBJECTIVE': 'SIP Payment',
'TRACKINGCYCLE': 5},
'ID': 61}]
我试图将其转换如下,但徒劳无功。
from collections import defaultdict
qr_dict = defaultdict(list)
for qr in query_result:
qr_dict[qr.ID].append(qr.qr)
我遇到以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-86-f76fa64c419a> in <module>
3 qr_dict = defaultdict(list)
4 for qr in query_result:
----> 5 qr_dict[qr.ID].append(qr.qr)
6
7
AttributeError: 'dict' object has no attribute 'ID'
答案 0 :(得分:0)
使用列表理解和dict.pop
例如:
query_result = [{'ID': 61, 'CAMP_DETAIL_ID': 1462, 'CAMP_START': datetime.datetime(2020, 1, 1, 0, 0), 'CAMP_END': datetime.datetime(2020, 2, 1, 0, 0), 'ISACTIVE': 1, 'ACTIVETRACKING': 1, 'TRACKINGCYCLE': 5, 'MAILSUBJECT': 'MCBAH Testing Campaign', 'CAMPAIGN_CODE': 'Testing', 'CAMPAIGN_DESCRIPTION': 'First Testing Campaign', 'ACCOUNT_SNO': 1810028081, 'CUSTOMER_EMAIL': 'm.fawadkhalid@gmail.com', 'MOBILE_NO': '923000704342', 'ACCOUNT_NAME': 'MOHAMMAD FAWAD KHALID', 'MAILSTATUS_APP': 'D', 'CAMPAIGN_OBJECTIVE_ID': 2, 'OBJECTIVE': 'SIP Payment', 'CAMPAIGN_DOCS_ID': 121, 'DOCUMENT': 'App_download_urdu_1.html', 'LAST_CYCLE': '2'}]
qr_dict = [{'ID':i.pop('ID'), 'data': i} for i in query_result]
输出:
[{'ID': 61,
'data': {'ACCOUNT_NAME': 'MOHAMMAD FAWAD KHALID',
'ACCOUNT_SNO': 1810028081,
'ACTIVETRACKING': 1,
'CAMPAIGN_CODE': 'Testing',
'CAMPAIGN_DESCRIPTION': 'First Testing Campaign',
'CAMPAIGN_DOCS_ID': 121,
'CAMPAIGN_OBJECTIVE_ID': 2,
'CAMP_DETAIL_ID': 1462,
'CAMP_END': datetime.datetime(2020, 2, 1, 0, 0),
'CAMP_START': datetime.datetime(2020, 1, 1, 0, 0),
'CUSTOMER_EMAIL': 'm.fawadkhalid@gmail.com',
'DOCUMENT': 'App_download_urdu_1.html',
'ISACTIVE': 1,
'LAST_CYCLE': '2',
'MAILSTATUS_APP': 'D',
'MAILSUBJECT': 'MCBAH Testing Campaign',
'MOBILE_NO': '923000704342',
'OBJECTIVE': 'SIP Payment',
'TRACKINGCYCLE': 5}}]