Парсинг массива объектов в бэкенд Django из Ajax-запроса
У меня есть некоторые данные, которые я собираю с помощью JavaScript и пытаюсь разобрать на бэкенде Django с помощью Ajax-запроса. Однако массив сильно отличается при консольном протоколировании в JavaScript и распечатке в python back end.
JavaScript:
function confirm_add_all_product(e) {
let data_products = []
let table = e.closest("thead").nextElementSibling.childNodes
table.forEach(function(i) {
let row = {}
row.record_id = (i.children[7].children[1].firstChild.getAttribute('data-record-id'))
row.product_id = (i.children[7].children[1].firstChild.getAttribute('data-product-id'))
row.foil = (i.children[2].textContent)
row.condition = (i.children[3].textContent)
row.language = (i.children[4].textContent)
row.quantity = (i.children[7].children[0].value)
data_products.push(row)
});
console.log(data_products)
$.ajax({
url: "/integrations/card/inventory_update_all/",
type: "POST",
data: {
csrfmiddlewaretoken: csrf_token,
data_products: data_products,
},
success: function (response) {},
error: function (request, status, error) {
console.log(error);
}
});
}
Python:
@login_required
def inventory_update_all(request):
print(request.POST)
return HttpResponse(status=200)
JS Console Log:
[
{
"record_id": "",
"product_id": "",
"foil": "false",
"condition": "nm",
"language": "eng",
"quantity": "1"
},
{
"record_id": "",
"product_id": "",
"foil": "false",
"condition": "nm",
"language": "eng",
"quantity": "2"
}
]
Текущая печать Python:
<QueryDict: {'csrfmiddlewaretoken': [''], 'data_products[0][record_id]': [''], 'data_products[0][product_id]': [''], 'data_products[0][foil]': ['false'], 'data_products[0][condition]': ['nm'], 'data_products[0][language]': ['eng'], 'data_products[0][quantity]': ['1'], 'data_products[1][record_id]': [''], 'data_products[1][product_id]': [''], 'data_products[1][foil]': ['false'], 'data_products[1][condition]': ['nm'], 'data_products[1][language]': ['eng'], 'data_products[1][quantity]': ['2']}>
Ожидаемая печать Python:
<QueryDict: {'csrfmiddlewaretoken': [''], 'data_products': {['record_id': '', 'product_id': '', 'foil': 'false', 'condition': 'nm', 'language': 'eng', 'quantity': '1'],['record_id': '', 'product_id': '', 'foil': 'false', 'condition': 'nm', 'language': 'eng', 'quantity': '2']}}>
Я хочу, чтобы данные в бэкенде python выглядели так, как ожидается, чтобы я мог в цикле просмотреть каждую запись data_products
и добавить ее в базу данных. Я не знаю, почему формат данных так сильно меняется между Ajax-запросом и бэкендом.