Hector
Hector

Reputation: 3

Multiple values JSON string Python (Django)

We have the following code working for receiving JSON strings for one value:

def spo2_new(request):
  if request.method == 'POST':
     data = {}
     fields = request.POST.copy()
     spo2 = SPO2(user=User.objects.get(pk=1), spo2=fields['value'], time=fields['datetime'])
     spo2.save()
     data['succes'] = True
    return JSONResponse(data)
raise Http404

The JSON string format POST is:

value=102.3&datetime=2011-05-04%2021:13:11

My question would be, how can we do the same for sending multiple "values" (with the datetime for each value) instead of sending each value individually?

Update 1: Since time interval is known, could I just send the values array and just the initial datetime?

Upvotes: 0

Views: 1205

Answers (2)

CraigKerstiens
CraigKerstiens

Reputation: 5954

Within your page you can build an array and post those values manually as stated below:

values=[[datetime1, value1],[datetime2,value2]]

Or you could loop over all items and build the list:

$.each($('.datetime'), function(i, item){
    datetime_values.append($(item).value());
}

Within your view you can get a list of values passed as:

request.POST.getlist('datetime')

Upvotes: 0

tokland
tokland

Reputation: 67850

Since you are already using JSON why don't simply send a JSON-encoded array of values [(timestamp, value)]?

POST: values=[[datetime1, value1],[datetime2,value2],...]

Upvotes: 1

Related Questions