1

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'),
2
  • in place of request.data.get('variable') use request.GET['variable'], like request.GET['TicketId'] Commented Feb 14, 2022 at 15:20
  • I have tried your approach now I'm getting an error as MultiValueDictKeyError at Data/SaveUserResponse/ 'AuditorId' @EvilReboot Commented Feb 14, 2022 at 15:27

1 Answer 1

1

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)
         ...
Sign up to request clarification or add additional context in comments.

8 Comments

I'm facing this error TypeError atapi/Data/SaveUserResponse/ the JSON object must be str, bytes or bytearray, not list
I guess request.data is already parsed.. in that case, notice that you have a list with one element.. I updated my answer
This approach I have used earlier as well this error I'm getting Attribute Error 'list' object has no attribute 'get'
can you print request.data? notice I added the [0] of the list..
Thanks mate able to fix it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.