2

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.

1 Answer 1

1

It was something very dumb. I have multiple ingestors all using this code but it seems that the multidimensional array threw that for a loop. What fixed my problem was in the request call. What i had originally was this:

response = requests.post(data_endpoint, data=temp, verify=False)

temp being the json formatted data. What fixed my problem was specifically doing a dumps on it and choosing the media type.

headers = {'Content-type': 'application/json'}
response = requests.post(data_endpoint, data=json.dumps(temp), verify=False, headers=headers)

Again i am not sure why the multi dimensional array forces my hand in this situation but that's what fixed my problem

Sign up to request clarification or add additional context in comments.

Comments

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.