Hi,
How can I access product, source and destination name instead of their “pk” in table. below code is working. In fact ser_order is below:
[{"model": "my_app.order", "pk": 220, "fields": {"sequence": 1172, "product": 1, "order_date": "2022-04-30", "source": 3, "destination": 4, "quantity": 23}}
]
However, I need to have their name instead of their primary key for product, source and destination.
Moreover, it try ser_order = {'id':order.id,'sequence':order.sequence,'product':order.product,'quantity':order.quantity, 'order_date':order.order_date,'source':order.source, 'destination':order.destination}
but it does not work.
order_instance = order_form.save(commit=True)
ser_order = serializers.serialize('json',[order_instance,])
return JsonResponse({'ser_order':ser_order}, status=200)
$(document).ready(function(){
// $('#current_order_info').DataTable();
$('#order_form').submit(function(e){
e.preventDefault();
var serializedData = $(this).serialize();
var url = document.querySelector('#order_form').dataset.url
// var url = "{% url 'my_app:add_order_regular_customer' %}"
$.ajax({
type: 'POST',
url: url,
data: serializedData,
success: function(response){
$('#order_form').trigger('reset');
var ser_order = JSON.parse(response['ser_order']);
var fields = ser_order[0];
$('#current_order_info').append(
`<tr>
<td>${fields["pk"]||""}</td>
<td>${fields['fields']["sequence"]||""}</td>
<td>${fields['fields']["product"]||""}</td>
<td>${fields['fields']["quantity"]||""}</td>
<td>${fields['fields']["order_date"]||""}</td>
<td>${fields['fields']["source"]||""}</td>
<td>${fields['fields']["destination"]||""}</td>
</tr>`
)
$('#current_order_info').DataTable();
},
});
});
});