I have a problem, I want to get all the data from the list
, so I want to loop through each data in the selected item and insert it into the database,
Currently when I print ['[object Object]', '[object Object]']
, it returns data like this,
How to insert these data one by one? Or just print them 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 the console selected_items is like this
So now I want to pass these lists 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)
It prints out like this
['[object Object]', '[object Object]']
Update codeHow to loop data? This is what I tried but it doesn't reflect the data at all
multiple_list: JSON.stringify(selected_items)
view.py
out_items = request.POST.get('multiple_list') for i in out_items: print(out_items1[i])
**How to print or insert into database?
When you do
selected_items.join(',')
, you are getting the__str__# of
{'stock_id': 5, 'quantity': 15}## (or equivalent js), which happens to be
[object Object ]So I recommend just using Json, which will encode the entire nested list dictionary and will be loaded as a normal list dictionary in python
edit
but! If you know that you are going to create each project, I recommend using bulk_create
&Additional Highlights
(It is very convenient to create filters dynamically ;))