温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Create nested dictionary from queryset

python - 从queryset创建嵌套字典

发布于 2020-03-29 21:53:33

我的查询集输出是

[{'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'

查看更多

提问者
Arif Khan
被浏览
18
Rakesh 2020-01-31 18:52

使用列表理解和 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}}]