Linked Questions

Popular Questions

How to past multiple list from ajax to Django

Asked by At

I have an issue which I want to get all the data from list, so I want to loop every single data from selected items and insert it to database, currently it returns the data like this when I print it ['[object Object]', '[object Object]'], how can I insert these data one by one? or just print it one by one?

I have this list which is selected_items I loop the data and then pass it to ajax

selected_items = []; 
for (var i = 0; i < checkBoxes.length; i++) {        
     var selected_obj ={ 
        stock_id: checkBoxes[i].id,
        quantity: row.cells[3].innerHTML
      }
      selected_items.push(selected_obj);
}

when console the selected_items it's just like this

enter image description here

so now I want to pass these list to django using ajax

console.log(selected_items);
  $.ajax({
      type: "POST",
      url: "{% url 'sales-item' %}",
      data:{
        multiple_list: selected_items.join(','), item_size: selected_items.length
      }
  }).done(function(data){...

views.py

out_items = request.POST.getlist('multiple_list[]')
print(out_items)

and it prints like this

['[object Object]', '[object Object]']

Updated Code how can I loop the data? this is what I tried but it doesn't reflect the data at all

multiple_list: JSON.stringify(selected_items)

viesw.py

out_items = request.POST.get('multiple_list')

for i in out_items:
     print(out_items1[i])

**how to print or insert it to the database?

Related Questions