Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attribute Error 'list' object has no attribute 'get' in Django

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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'),

>Solution :

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)
         ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading