I'm trying to serialize an array that comes from a form. The array looks like this:
some_id: 12
array1[]:1
array1[]:2
array2[]:5
array2[]:6
or, as it arrives in request.POST
<QueryDict: {
'array1[]': ['1', '2'],
'array2[]': ['5', '6'], ...
Now I get the serializer working for the simple id: some_id = serializers.IntegerField()
, which is great. I've tested it without the arrays and everything works as expected.
For the array I thought I found this serializer field: listfield
I tried the following implementation:
array1 = serializers.ListField(
child=serializers.IntegerField(min_value=1)
)
This looks like the manual says how it works, but it only seems to give me the following error:
TypeError at /api/myendpoint/ 'int' object is not iterable
I'm not really sure where I went wrong, maybe I'm on the wrong track (really not a Django guy :) ), but I'm sure it's a small mistake.
So how can I serialize a posted array (or should I call it a Dict?)
The complete trace doesn't seem to be containing anything strange, but i've kept it available on pastebin
The data is posted with this jquery code:
var mydata = {
array1: [1,2],
array2: [5,6],
some_id: 12
};
$.ajax({
url: "http://domain.com//api/myendpoint/",
dataType: "json",
data: mydata,
method: "POST"
})
I'm using:
- Django 1.10.2
- djangorestframework 3.5.2
- jquery 2.1.4