Home > Enterprise >  Attribute Error 'list' object has no attribute 'get' in Django
Attribute Error 'list' object has no attribute 'get' in Django

Time:02-15

I'm facing a issue while getting the data from the 'POST' method I'm getting an error as Attribute Error at api/Data/SaveUserResponse/ 'list' object has no attribute 'get' Django .

The response which I get in the 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):
    if request.method == 'POST':
    
        auditorid = request.data.get('AuditorId')
        print('auditorid---', auditorid)
        ticketid = request.data.get('TicketId')
        qid = request.data.get('QId')
        answer = request.data.get('Answer')
        sid = '0'
        TicketType = request.data.get('TicketType')
        TypeSelected = request.data.get('TypeSelected')
        agents = request.data.get('Agents')
        supervisor = request.data.get('Supervisor')
        Comments = request.data.get('Comments')
        action = request.data.get('Action')
        subfunction = request.data.get('AuditSubFunction')
        region = request.data.get('AuditRegion')
        print('Region---', region)
        

        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)

urls.py:

 path('Data/SaveUserResponse/', SaveUserResponse, name='SaveUserResponse'),

CodePudding user response:

request.data is a list with one element based on your error, you need the first element.

You can use json.loads to do it:

@api_view(['POST',])
def SaveUserResponse(request):
    if request.method == 'POST':      
         auditorid = request.data[0].get('AuditorId') # notice the [0]
         print('auditorid---', auditorid)
         ...
  • Related