I'm trying to return all the results from the SQL using this approach I'm able to retrieve it but its the static one.
I have tried looping the result_set
but its returning only the first response out of 5 responses but I want to return all the 5 response
Here, What I have tried
expected response:
[{"Value8":"Avail-On queue","Value1":"Avail-On queue"},{"Value8":"Coaching","Value1":"Coaching"},{"Value8":"Huddle","Value1":"Huddle"},{"Value8":"Outage","Value1":"Outage"},{"Value8":"Training","Value1":"Training"}]
views.py:
@api_view(['GET'])
def GetClaimsActivity(request, UserId):
if request.method == 'GET':
cursor = connection.cursor()
cursor.execute('EXEC [dbo].[sp_GetClaimsPlusActivity] @UserId=%s', (UserId,))
result_set = cursor.fetchall()
data = []
# for row in result_set:
#data.append({
#'Value8':row[0],
#'Value1':row[0],
#})
#return Reponse(data)
data = []
data.append({
'Value8':result_set[0][0],
'Value1':result_set[0][0],
})
data.append({
'Value8':result_set[1][0],
'Value1':result_set[1][0],
})
data.append({
'Value8':result_set[2][0],
'Value1':result_set[2][0],
})
data.append({
'Value8':result_set[3][0],
'Value1':result_set[3][0],
})
data.append({
'Value8':result_set[4][0],
'Value1':result_set[4][0],
})
return Response(data)
CodePudding user response:
Try this
@api_view(['GET'])
def GetClaimsActivity(request, UserId):
if request.method == 'GET':
cursor = connection.cursor()
cursor.execute('EXEC [dbo].[sp_GetClaimsPlusActivity] @UserId=%s', (UserId,))
result_set = cursor.fetchall()
data = []
for i in range(len(result_set)):
data.append({
'Value8':result_set[i][0],
'Value1':result_set[i][0],
})
return Reponse(data)```