imran_44
imran_44

Reputation: 329

list indices must be integers or slices, not dict in django

I just want to iterate through the list of JSON data which I get in the payload but getting an error as list indices must be integers or slices, not dict

payload:

  [{"AuditorId":10,"Agents":"sa","Supervisor":"sa","TicketId":"58742","QId":150,"Answer":"Yes","TypeSelected":"CMT Mails","Comments":"na","TicketType":"Regularticket","Action":"na","AuditSubFunction":"na","AuditRegion":"na"},{"AuditorId":10,"Agents":"sa","Supervisor":"sa","TicketId":"58742","QId":151,"Answer":"Yes","TypeSelected":"CMT Mails","Comments":"na","TicketType":"Regularticket","Action":"na","AuditSubFunction":"na","AuditRegion":"na"}]

views.py:

@api_view(['POST'])
def SaveUserResponse(request):

    for ran in request.data:
        
        auditorid = request.data[ran].get('AuditorId')
        ticketid = request.data[ran].get('TicketId')
        qid = request.data[ran].get('QId')
        answer = request.data[ran].get('Answer')
        sid = '0'
        TicketType = request.data[ran].get('TicketType')
        TypeSelected = request.data[ran].get('TypeSelected')
        agents = request.data[ran].get('Agents')
        supervisor = request.data[ran].get('Supervisor')
        Comments = request.data[ran].get('Comments')
        action = request.data[ran].get('Action')
        subfunction = request.data[ran].get('AuditSubFunction')
        region = request.data[ran].get('AuditRegion')
       
        cursor = connection.cursor()
        cursor.execute('EXEC [dbo].[sp_SaveAuditResponse] @auditorid=%s,@ticketid=%s,@qid=%s,@answer=%s,@sid=%s,@TicketType=%s,@TypeSelected=%s,@agents=%s, @supervisor =%s, @Comments=%s, @action=%s, @subfunction=%s, @region=%s',
         (auditorid,ticketid,qid,answer, sid,TicketType, TypeSelected, agents, supervisor, Comments, action, subfunction,region))
        return Response(True)
      

Upvotes: 0

Views: 93

Answers (1)

Bartosz Stasiak
Bartosz Stasiak

Reputation: 1632

I ran this code on my machine and it works for the payload you provided.

@api_view(['POST'])
def SaveUserResponse(request):

    for ran in request.data:
        auditorid = ran.get('AuditorId')
        ticketid = ran.get('TicketId')
        qid = ran.get('QId')
        answer = ran.get('Answer')
        sid = '0'
        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')

If it doesn't then content of request.data must be different then payload you shared in original post

Upvotes: 2

Related Questions