-1

I'm trying to iterate through the list of JSON, it just iterate only the first row How could I able to loop through all the rows inside the list

This is my payload in browser

   [{AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…},…]
0: {AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…}
1: {AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…}
2: {AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…}
3: {AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…}
4: {AuditorId: 10, Agents: Joshi", Supervisor: Prabhu", TicketId: "R6726587",…}

here, what I tried

@api_view(['POST'])
def UserResponse(request):     
  if request.method == 'POST': 
    for ran in request.data:
        auditorid =ran.get('AuditorId')
        ticketid = ran.get('TicketId')
        qid = ran.get('QId')
        answer = ran.get('Answer')
        sid =  ran.get('SID')
        TicketType = ran.get('TicketType')
        TypeSelected = ran.get('TypeSelected')
        agents = ran.get('Agents')
        supervisor = ran.get('Supervisor')
        Comments = ran.get('Comments')
        action = ran.get('Action')
        subfunction = ran.get('AuditSubFunction')
        region = ran.get('AuditRegion')
        Qstans = str(qid)+'|'+ answer+'&&'

        cursor = connection.cursor()
        cursor.execute('EXEC [dbo].[sp_SaveAuditResponse] @auditorid=%s,@agents=%s,@supervisor=%s,@ticketid=%s,@Qstans=%s,@sid=%s,@TicketType=%s,@TypeSelected=%s, @Comments =%s, @action=%s, @subfunction=%s, @region=%s',
         (auditorid,agents,supervisor,ticketid, Qstans,sid, TicketType, TypeSelected, Comments, action, subfunction,region))
        result_st = cursor.fetchall()
        for row in result_st:
            return Response({0:row[0]})
kelvin003
  • 31
  • 8
  • can you just print ```request.data``` – shivankgtm May 18 '22 at 10:57
  • 2
    You are returning a Response at the end of your route. That's why you only iterate one time. You should store the result of your db query into a list, and when the request.data iteration is finished, return your Response there. – Bloodbee May 18 '22 at 10:57
  • @Bloodbee could u pls show in the code it would be helpful – kelvin003 May 18 '22 at 11:01
  • @shivankgtm when I print `request.data` I could able to get all the data but in response I get only one – kelvin003 May 18 '22 at 11:08
  • Hi, there is a reason for not use django orm? What do you want to accomplish with this query? – Luiz May 18 '22 at 11:13
  • I work on the Django project so, I don't have any other choice. Just I have stucked in this @Luiz. If possible could you pls help – kelvin003 May 18 '22 at 11:17
  • @kelvin003 just paste the request.data , I want to see what type of dict you are getting. – shivankgtm May 18 '22 at 12:18
  • `[{'AuditorId': 10, 'Agents': 'Joshi', 'Supervisor': 'Prabhu', 'TicketId': 'HRR6571802', 'QId': 150, 'Answer': 'Yes', 'SID': 0, 'TypeSelected': 'CMT Mails', 'Comments': 'test', 'TicketType': 'Regularticket', 'Action': '5', 'AuditSubFunction': 'test', 'AuditRegion': 'test'},..]` @shivankgtm – kelvin003 May 18 '22 at 12:44
  • Does this answer your question? [How to use a return statement in a for loop?](https://stackoverflow.com/questions/44564414/how-to-use-a-return-statement-in-a-for-loop) – Abdul Aziz Barkat May 18 '22 at 13:00

1 Answers1

0

Store your results into a list and them return as response, if you need to use cursor i think it's better to open connection before the loop

@api_view(['POST'])
def UserResponse(request):     
  if request.method == 'POST': 
    data_to_return = []
    cursor = connection.cursor()
    for ran in request.data:
        auditorid =ran.get('AuditorId')
        ticketid = ran.get('TicketId')
        qid = ran.get('QId')
        answer = ran.get('Answer')
        sid =  ran.get('SID')
        TicketType = ran.get('TicketType')
        TypeSelected = ran.get('TypeSelected')
        agents = ran.get('Agents')
        supervisor = ran.get('Supervisor')
        Comments = ran.get('Comments')
        action = ran.get('Action')
        subfunction = ran.get('AuditSubFunction')
        region = ran.get('AuditRegion')
        Qstans = str(qid)+'|'+ answer+'&&'
        cursor.execute('EXEC [dbo].[sp_SaveAuditResponse] @auditorid=%s,@agents=%s,@supervisor=%s,@ticketid=%s,@Qstans=%s,@sid=%s,@TicketType=%s,@TypeSelected=%s, @Comments =%s, @action=%s, @subfunction=%s, @region=%s',
         (auditorid,agents,supervisor,ticketid, Qstans,sid, TicketType, TypeSelected, Comments, action, subfunction,region))
        result_st = cursor.fetchall()
        for row in result_st:
            data_to_return.append(row)
    return Response(data_to_return)
Luiz
  • 1,985
  • 6
  • 16