我无法访问我从JSON API获取的某些字段,如果用户是多个on_call策略的一部分,我在API中不知道如何访问第二个或更多策略。我目前的代码只是在网络团队政策下两次返回Bob Brown,而不是在网络团队和solarwinds测试下两次bob brown
JSON示例
{
"users": [{
"id": "PN8PIFR",
"name": "Bob Brown",
"user_url": "/users/PN8PIFR",
"invitation_sent": false,
"marketing_opt_out": false,
"on_call": [{
"level": 2,
"start": "2016-05-30T08:00:00Z",
"end": "2016-06-06T08:00:00Z",
"escalation_policy": {
"id": "PYZTEI3",
"name": "Network Team"
}
}, {
"level": 1,
"start": null,
"end": null,
"escalation_policy": {
"id": "P3CBIDO",
"name": "Solarwinds Test"
}
}],
"job_title": "Network Engineer"
}, {
"id": "PLIMHAI",
"name": "Bill Blue",
"user_url": "/users/PLIMHAI",
"invitation_sent": false,
"marketing_opt_out": false,
"on_call": [{
"level": 1,
"start": "2016-06-02T08:00:00Z",
"end": "2016-06-03T06:00:00Z",
"escalation_policy": {
"id": "PARVPQB",
"name": "DBA Team"
}
}],
"job_title": "Database Administrator"
}],
"active_account_users": 9,
"limit": 25,
"offset": 0,
"total": 6
}
view.py
class User(object):
__attrs = ['PolicyID','Level','LevelNo', 'StartDate', 'EndDate', 'StartTime',
'EndTime', 'Name', 'Mobile']
def __init__(self, **kwargs):
for attr in self.__attrs:
setattr(self, attr, kwargs.get(attr, None))
def __repr__(self):
return ', '.join(
['%s: %r' % (attr, getattr(self, attr)) for attr in self.__attrs])
### Get the Policy data ###
objPolicyData = getuser_data()
pdUsers = pdUser.objects.all()
### Index View ###
@login_required
def index(request):
OnCallData = []
for user_item_data in objPolicyData['users']:
user_data = User()
#get the user object from DB
model_pd_user = pdUser.objects.get(PagerDutyID=user_item_data['id'])
user_data.Name = model_pd_user.Name
user_data.Mobile = model_pd_user.Mobile
for on_call_data in user_item_data['on_call']:
user_data.LevelNo = on_call_data['level']
user_data.Level = getLevel(on_call_data['level'])
user_data.StartDate = getDate(on_call_data['start'])
user_data.EndDate = getDate(on_call_data['end'])
user_data.StartTime = getTime(on_call_data['start'])
user_data.EndTime = getTime(on_call_data['end'])
user_data.PolicyID = on_call_data['escalation_policy']['id']
OnCallData.append(user_data)