我正在做一些sqlqueries用於從數據庫表中獲取項目。字典返回問題
然後我用相同的方式創建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
出於調試目的,我已經印刷了DATAS,同樣是如下:
########## {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}}
有人讓我知道獲得這兩個細節的方式。
我需要修改退貨部分,告訴我相同的可行和經濟的方法。
我不明白上面的代碼應該和什麼不工作。 – martin
@Martin。 謝謝你的時間。我從下面的人回答瞭解答。 – iamnewuser