'Attribute Error: 'list' object has no attribute 'get'
I'm trying to get the data from the POST method, it's JSON inside the list. Whenever I tried to read the JSON I'm getting an error as ticketid = request.data.get('TicketId') AttributeError: 'list' object has no attribute 'get'
Here, what I have tried
views.py
@api_view(['POST'])
def SaveUserResponse(request):
if request.method == 'POST':
auditorid =request.data.get('AuditorId')
print('SaveUserResponse auditorid---', auditorid)
ticketid = request.data.get('TicketId')
qid = request.data.get('QId')
answer = request.data.get('Answer')
sid = 0
cursor = connection.cursor()
cursor.execute('EXEC [dbo].[sp_SaveAuditResponse] @auditorid=%s,@ticketid=%s,@qid=%s,@answer=%s,@sid=%s',
(auditorid,ticketid,qid,answer, sid,))
result_st = cursor.fetchall()
for row in result_st:
print('sp_SaveAuditResponse', row[0])
return Response(row[0])
return sid
Payload data:
[
0: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 1, Answer: "2", SID: "0",…}
1: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 2, Answer: "2", SID: "0",…}
2: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 3, Answer: "2", SID: "0",…}
3: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 4, Answer: "2", SID: "0",…}
4: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 5, Answer: "5", SID: "0",…}
5: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 6, Answer: "5", SID: "0",…}
6: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 7, Answer: "3", SID: "0",…}
7: {AuditorId: 122, Agents: "", Supervisor: "", TicketId: "12111", QId: 8, Answer: "3", SID: "0",…}
]
Solution 1:[1]
As Willem Van Onsem pointed out, your request.data is a list, so you cannot use .get. You need to access a specific dictionary from this list in order to use .get.
I'm not exactly sure which TicketId you want but if it isn't important because they are all the same, you could do:
request.data[0].get('TicketId')
If you want all Ticked IDs as list, you can use list comprehension
[d['TicketId'] for d in request.data]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
