Home  >  Q&A  >  body text

How to pass multiple lists from ajax to Django

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?

P粉068174996P粉068174996179 days ago389

reply all(1)I'll reply

  • P粉680487967

    P粉6804879672024-04-04 14:29:06

    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

    Javascript

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

    Python

    stock_list = json.loads(request.POST.get('multiple_list'))
    
    print(type(stock_list))
    # 
    
    print(stock_list)
    # [
    #   {'stock_id': 5, 'quantity': 15},
    # ]
    


    edit

    Yes! , you just loop over it like a normal nested list dictionary

    stock_list = json.loads(request.POST.get('multiple_list'))
    
    for stock_list_item in stock_list:
      obj, was_created_bool = MyModel.objects.get_or_create(
        stock_id=stock_list_item['stock_id'],
        quantity=stock_list_item['quantity']
      )
    

    but! If you know that you are going to create each project, I recommend using bulk_create

    stock_list = json.loads(request.POST.get('multiple_list'))
    
    bulk_create_list = []
    
    for stock_list_item in stock_list:
      bulk_create_list.append(
        MyModel(  # Note: NO .object
          stock_id=stock_list_item['stock_id'],
          quantity=stock_list_item['quantity']
        )
      )
    
    # Creates all items in one query
    MyModel.objects.bulk_create(bulk_create_list)
    


    &Additional Highlights

    The working principles of these are the same!

    (It is very convenient to create filters dynamically ;))

    # setup
    stock_list_item = {'stock_id': 5, 'quantity': 15}
    # ----
    
    MyModel.objects.get_or_create(
      stock_id=stock_list_item['stock_id'],
      quantity=stock_list_item['quantity']
    )
    # ==
    MyModel.objects.get_or_create(**{'stock_id': 5, 'quantity': 15})
    # ==
    MyModel.objects.get_or_create(**stock_list_item)
    

    reply
    0
  • Cancelreply