TLDR: trying to send a 2d array with django rest and it fails, why?
So here is my situation. I am creating a ingest tool to take in data, that is created by someone else. Part of this data is a 2d array or a list of lists, whatever strikes your fancy. Something like this being a list of lists of floats in this case
Data = [[1,2,3,4,5,6],[1,2,3,4,5,6]]
I am trying to use a Django rest service to do this because thats what i have been using for everything else i have done. Anyway this data type is given to me in a json format along with other datatypes Which i will show as well. On the model side of this i have a model of the main dataType
that data
would go into which looks like this
class dataType(models.Model):
data = ArrayField(ArrayField(models.FloatField(blank=True, null=True),
null=True), null=True)
data1 = ArrayField(models.FloatField(blank=True, null=True),
null=True)
...
other data
Now data
is the one i am having issues with. data1
will work fine as long as its just singular dimensional array(list). when i run the code with this i get an error saying this Expected a list of items but got type \\"str\\"."]}'
even though i am sending it a list. It gets a 400 for this vary reason. Here is what the json code would look like.
"dataType": [
{
"otherData": stuff
"data": [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6]
]
}
]
Just as side not before anyone asks, no i cannot change the data from a 2d array, it has to stay like that, if it were up to me i would change it.
Another side note, i have attempted using just one arrayfield like data1
with the 2d array stuff, it returns back a 201 BUT on the rest side when i go to localhost:8000/api/v3/dataType it gives me a 500 error and complains and says something along the lines of looking for a string or float but received a list.
UPDATE: concerning the last side note, i have found this does work but it converts my 2d array into one giant single array so this is still not what i want, i need my 2d array to stay as a 2d array.
Copyright Notice:Content Author:「that_one_nerdy_guy」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49880159/multidimensional-array-model-django