我正在使用simple_salesforce
来查询CRM关系数据库。使用SOQL
查询数据,SQL
是Salesforce的None
变体。
它返回嵌套的有序词典。在“连接”上,如果没有记录,则返回OrderedDict
。如果有记录,则返回select Id, Subject, Account.Name, Opportunity.Name From Case where ID = '500xxxxxxxxxxxxx'
。我所拥有的循环似乎不是最有效的,因为它重复几行。我正在寻找一种更清晰的方式来编写循环。
使用的查询:
from collections import OrderedDict
od = OrderedDict([
(u'totalSize', 1),
(u'done', True),
(u'records', [OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/data/v29.0/sobjects/Case/500xxxxxxxxxxxxxxxx')
])
),
(u'Id', u'500xxxxxxxxxxxxxxxx'),
(u'Subject', u"Sample Case 1"),
(u'Account', OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Account'),
(u'url', u'/services/data/v29.0/sobjects/Account/001xxxxxxxxxxxxxxxx')
])
),
(u'Name', u'Sample Account 1')
])),
(u'Opportunity', None)]
)])])
output = []
def get_items(this_item):
for key, value in this_item.iteritems():
if key <> 'attributes':
if type(value) is OrderedDict:
for key, value in value.iteritems():
if key <> 'attributes':
record.append(str(value))
else:
record.append(str(value))
for item in od['records']:
record = []
record.append(get_items(item))
output.append(record)
print output
代码:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', 'None']]
输出:
<li ng-repeat="x in products" ng-click="toggleSelection(x)" ng-style="{ 'color' : (x.alreadyTook) ? 'red' : 'black' }">{{x.item}}
</li>
答案 0 :(得分:1)
试试这个:
def get_rec_vals(rec):
ret_list = []
for key,val in rec.items():
if key == 'attributes': continue
if isinstance(val, OrderedDict):
# recursive call
ret_list.extend(get_rec_vals(val))
else:
ret_list.append(val)
return ret_list
output = [get_rec_vals(rec) for rec in od['records']]
print(output)
输出:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', None]]
PS我不确定它是否适用于多个记录,因此您需要提供至少两个记录...