[Fixed]-Ajax call in Django Rest Framework using array

1👍

@list_route(methods=['POST'], permission_classes=[AllowAny])
def test(self, request):
    print(request.data)
    print([(x, y) for x, y in request.data.items()])
    print(request.data['owner'])
    import ast
    for x in ast.literal_eval(request.data['owner']):
        print(x)
    return Response("...")

<QueryDict: {'address': ['foo'], 'owner': ['[1,2]']}>
[('address', 'foo'), ('owner', '[1,2]')]
[1,2]
1
2

Ajax try:

data = {
 address: 'foo'
 owner: '[1,2]'
}
👤Ykh

0👍

I have the same problem, i solved this problem providing

serializer = TestSerializer(data=request.data.dict())

instead o f

serializer = TestSerializer(data=request.data)

I supose this inconvenient will be solved in a future release.

Leave a comment