Dict回归问题

时间:2016-02-23 10:25:59

标签: python json dictionary

我正在做一些sqlqueries来从Database表中获取项目。

然后我用相同的方式创建Dict。

将完成json.dumps和dict操作的一些额外操作。

之后我需要返回结果。

我在返回结果时面临问题。

例如,请在此处考虑我需要返回两个租户的结果,但我无法正确执行此操作。

实施代码,只返回一个租户的详细信息。

请参阅此处的代码:

   r = q.all()

    # For getting the values of sql obj
    for m in r:

            n = {}

            g = m.__dict__

            g.pop('_sa_instance_state', None)

            tenant_id = g['tenant_id']

            n[tenant_id] = g

            print("##########")
            print(n)
            print("#########")

            n = json.dumps(n, default=json_util.default)

    return n

出于调试目的,我打印了数据,同样如下:

##########
{u'7b73b9644e8242b3a740afc4659d9829': {'balance_cost': None, 'invoice_id': u'demo-1-2016', 'paid_cost': None, 'invoice_data': u'{"dict_all_cost_total": "109776.13801", "dict_total_all": {"5477b2b2-91f8-4e4b-a5b0-8f4a32210915": ["instance_for_demo", "m1.tiny", "109776.13801"]}, "dict_inbound": {"5477b2b2-91f8-4e4b-a5b0-8f4a32210915": ["instance_for_demo", "m1.tiny", "93072.0652571"]}, "dict_volume": "0", "dict_compute": {"5477b2b2-91f8-4e4b-a5b0-8f4a32210915": ["instance_for_demo", "m1.tiny", "16590.0"]}, "dict_floating": "0", "dict_outbound": {"5477b2b2-91f8-4e4b-a5b0-8f4a32210915": ["instance_for_demo", "m1.tiny", "114.072752953"]}}', 'payment_status': None, 'total_cost': 109776.0, 'invoice_date': datetime.datetime(2016, 2, 20, 1, 4, 16), 'invoice_period_to': datetime.datetime(2016, 2, 19, 1, 4, 16), 'tenant_id': u'7b73b9644e8242b3a740afc4659d9829', 'invoice_period_from': datetime.datetime(2016, 1, 20, 1, 4, 16), 'tenant_name': u'demo', 'id': 1}}
#########
##########
{u'b77865b66fd544e0841aa7dbca8bdc97': {'balance_cost': None, 'invoice_id': u'tenant_for_test-1-2016', 'paid_cost': None, 'invoice_data': u'{"dict_all_cost_total": "16872.4061451", "dict_total_all": {"c3362ca0-8810-4ab7-a5b2-3cb571815690": ["test_instance", "m1.tiny", "16797.8795147"]}, "dict_inbound": {"c3362ca0-8810-4ab7-a5b2-3cb571815690": ["test_instance", "m1.tiny", "98.6362838745"]}, "dict_volume": "0", "dict_compute": {"c3362ca0-8810-4ab7-a5b2-3cb571815690": ["test_instance", "m1.tiny", "16570.0"]}, "dict_floating": "4.5", "dict_outbound": {"c3362ca0-8810-4ab7-a5b2-3cb571815690": ["test_instance", "m1.tiny", "129.24323082"]}}', 'payment_status': None, 'total_cost': 16872.4, 'invoice_date': datetime.datetime(2016, 2, 20, 1, 4, 16), 'invoice_period_to': datetime.datetime(2016, 2, 19, 1, 4, 16), 'tenant_id': u'b77865b66fd544e0841aa7dbca8bdc97', 'invoice_period_from': datetime.datetime(2016, 1, 20, 1, 4, 16), 'tenant_name': u'tenant_for_test', 'id': 2}}

但是当我试图获得结果时,它只返回第二个租户的细节:

{"b77865b66fd544e0841aa7dbca8bdc97": {"balance_cost": null, "invoice_id": "tenant_for_test-1-2016", "paid_cost": null, "invoice_data": "{\"dict_all_cost_total\": \"16872.4061451\", \"dict_total_all\": {\"c3362ca0-8810-4ab7-a5b2-3cb571815690\": [\"test_instance\", \"m1.tiny\", \"16797.8795147\"]}, \"dict_inbound\": {\"c3362ca0-8810-4ab7-a5b2-3cb571815690\": [\"test_instance\", \"m1.tiny\", \"98.6362838745\"]}, \"dict_volume\": \"0\", \"dict_compute\": {\"c3362ca0-8810-4ab7-a5b2-3cb571815690\": [\"test_instance\", \"m1.tiny\", \"16570.0\"]}, \"dict_floating\": \"4.5\", \"dict_outbound\": {\"c3362ca0-8810-4ab7-a5b2-3cb571815690\": [\"test_instance\", \"m1.tiny\", \"129.24323082\"]}}", "payment_status": null, "total_cost": 16872.4, "invoice_date": {"$date": 1455930256000}, "invoice_period_to": {"$date": 1455843856000}, "tenant_id": "b77865b66fd544e0841aa7dbca8bdc97", "invoice_period_from": {"$date": 1453251856000}, "tenant_name": "tenant_for_test", "id": 2}}

有人告诉我获取细节的方法。

我需要在返回部分进行修改,请告诉我相同的可行和经济的方法。

1 个答案:

答案 0 :(得分:1)

您在每次迭代时初始化并转储dict n,丢失所有以前的添加内容。在循环之前执行此操作,并在循环完成后将其转储。