I am creating sales invoice where I create one form with two database table connections like sales details are saving in Sales_Invoice table and Items which we add in invoice will save in Item_Details table.
So here in html form I am creating table for add items after adding that I items I wish to save sales invoice in sales_details and all items in Item_Details by using sales_invoice foreign key
My data is saving but only one row is saving How can I save multiple data in django by using queryset
Html Form
<form method="post">
{% csrf_token %}
<!-- Estimate Details -->
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>Invoice Number</label>
<input type="text" class="form-control" placeholder={{invoice_number}} name="invoice_number">
</div>
<div class="col-md mb-2">
<label>Date</label>
<input type="date" class="form-control" placeholder="Invoive_Date" name="invoice_date">
</div>
<div class="col-md mb-2">
<label>Select Customer</label>
<select id="customer_id" class="form-control" name="customer_id">
<option value="">Select Customer</option>
{% if all_customers %}
{% for c in all_customers %}
<option value="{{c.customer_id}}">{{c.first_name}} {{c.surname}}</option>
{% endfor %}
{% endif %}
</select>
</div>
</div>
<div class="row pb-3">
<div class="col-md-4 mb-2">
<label>Contact Person Name</label>
<input type="text" class="form-control" id="contact_person_name" name="contact_person_name"
placeholder="Contact Person">
</div>
<div class="col-md-4 mb-2">
<label>Phone</label>
<input type="number" class="form-control" placeholder="Phone" id="phone" name="phone">
</div>
</div>
<div class="row pb-3">
<div class="col-md mb-2">
<label>Bill To</label>
<textarea type="text" class="form-control" placeholder="Bill To" id="bill_to"
name="bill_to"></textarea>
</div>
<div class="col-md mb-2">
<label>Ship To</label>
<textarea type="text" class="form-control" placeholder="Ship To" id="ship_to"
id="ship_to"></textarea>
</div>
<div class="col-md mb-2">
<label>Special Instructions</label>
<textarea type="text" class="form-control" placeholder="Special Instructions"
id="special_instructions" name="special_instructions"></textarea>
</div>
</div>
<!-- Items -->
<div class="card-header">
<div class="row">
<div class="col-auto m-1 pl-2">
<img src="{% static 'images/GIF/Add.gif'%}" width="30px" height="30px">
</div>
<div class="col mt-2">
<h5>Item Details</h5>
</div>
</div>
</div>
<div>
<!-- <form> -->
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>Item</label>
<select class="form-control" placeholder="Items" id="item" name="item">
<option value="">Item</option>
{% if all_items %}
{% for i in all_items %}
<option value="{{i.item}}">{{i.item}}</option>
{% endfor %}
{% endif %}
</select>
</div>
<div class="col-md mb-2">
<label>Unit</label>
<select class="form-control" placeholder="Units" id="unit_name" name="unit_name">
<option value="">Unit</option>
{% if all_unit %}
{% for u in all_unit %}
<option value="{{u.unit_name}}">{{u.unit_name}}</option>
{% endfor %}
{% endif %}
</select>
</div>
<div class="col-md mb-2">
<label>Unit Price</label>
<input type="number" class="form-control" placeholder="Unit Price" id="unit_price"
name="unit_price">
</div>
<div class="col-md mb-2">
<label>Quantity</label>
<input type="number" class="form-control" placeholder="Quantity" onchange="quantityChange()" id="quantity"
name="quantity">
</div>
<div class="col-md mb-2">
<label>HSN</label>
<input type="text" class="form-control" placeholder="HSN" id="hsn" name="hsn">
</div>
<div class="col-md mb-2">
<label>Tax</label>
<input type="text" class="form-control" placeholder="Tax" id="tax_code" name="tax_code">
</div>
<div class="col-md mb-2">
<label>Amount</label>
<input type="number" class="form-control" readonly placeholder="Amount" id="amount" name="amount">
</div>
<div class="col-auto">
<P><button type="button" class="w3-btn w3-blue" id="submit">Save</button>
</div>
</div>
<!-- </form> -->
</div>
<br>
<!-- Items table -->
<div id="tab">
<div class="table-responsive-md">
<table class="table table-sm" style="background-color: #e4f7f5;" id="demo">
<thead>
<tr>
<th nowrap>Items</th>
<th nowrap>HSN</th>
<th nowrap>Quantity</th>
<th nowrap>Unit</th>
<th nowrap>Unit Price</th>
<th nowrap>Tax</th>
<th nowrap>Amount</th>
<th nowrap>Action</th>
</tr>
</thead>
<tbody id="items_tbody"></tbody>
<tfoot>
<tr>
<th colspan="5"></th>
<th id="total">Total :</th>
<td id="total_value"></td>
</tr>
<tr>
<th colspan="5"></th>
<th id="CGST">CGST :</th>
<td>200</td>
</tr>
<tr>
<th colspan="5"></th>
<th id="SGST">SGST :</th>
<td>200</td>
</tr>
<tr>
<th colspan="5"></th>
<th id="discount">Discount :</th>
<td>200</td>
</tr>
<tr>
<th colspan="5"></th>
<th id="round_off">Round Off :</th>
<td>200</td>
</tr>
<tr>
<th colspan="5"></th>
<th id="grand_total">Grand Total :</th>
<td>200</td>
</tr>
</tfoot>
</table>
</div>
</div>
<!-- Other Details -->
<div class="card-header">
<div class="row">
<div class="col-auto m-1 pl-2">
<img src="../static/images/GIF/Setting.gif" width="30px" height="30px">
</div>
<div class="col mt-2">
<h5>Other Details</h5>
</div>
</div>
</div>
<div class="row pb-3 pt-3">
<div class="col-md mb-2">
<label>User</label>
<input type="text" class="form-control" placeholder={{username}} id="user" name="username">
</div>
<div class="col-md mb-2">
<label>Remarks</label>
<textarea type="text" class="form-control" placeholder="Remarks" name="remarks"></textarea>
</div>
</div>
<div class="row">
<div class="col">
<button class="btn btn-primary" onclick=SaveData()>Save</button>
</div>
</div>
</form>
<div id="myDiv"></div>
</div>
</div>
<br>
</div>
<br>
<scripts>
//Add Items
var items = [];
$("#submit").on("click", function () {
var temp =
{
"item" : document.getElementById("item").value,
"hsn" : document.getElementById("hsn").value,
"quantity" : document.getElementById("quantity").value,
"unit_name" : document.getElementById("unit_name").value,
"unit_price" : document.getElementById("unit_price").value,
"tax_code" : document.getElementById("tax_code").value,
"amount":document.getElementById("amount").value,
};
items.push(temp);
console.log(items)
$("#demo > tbody").empty();
var total_amount = 0
for (var i = 0; i < items.length; i++) {
document.getElementsByTagName("tbody")[0].innerHTML+= "<tr id='row" + i +"'><td>"+items[i].item+"</td><td>"+items[i].hsn+"</td><td>"+items[i].quantity+"</td><td>"+items[i].unit_name+"</td><td>"+items[i].unit_price+"</td><td>"+items[i].tax_code+"</td><td>"+items[i].amount+"</td><td><button class='remCF1 btn btn-small btn-danger'" +
"onClick='delRow(" + i + ")' name='delButton'>Delete</button></td></tr>"
console.log(items[i].amount)
items[i].amount = parseInt(items[i].amount)
total_amount = total_amount + items[i].amount
$('#total_value').text(total_amount);
};
});
function quantityChange(){
var base_Amount = parseInt(document.getElementById("quantity").value) * parseInt(document.getElementById("unit_price").value);
$("#amount").val(base_Amount);
}
function delRow(item) {
this.items.splice(item, 1);
document.getElementById("row" + item).remove();
console.log(items)
$("#demo > tbody").empty();
for (var i = 0; i < items.length; i++) {
document.getElementsByTagName("tbody")[0].innerHTML+= "<tr id='row" + i +"'><td>"+items[i].item+"</td><td>"+items[i].hsn+"</td><td>"+items[i].quantity+"</td><td>"+items[i].unit_name+"</td><td>"+items[i].unit_price+"</td><td>"+items[i].tax_code+"</td><td>"+items[i].amount+"</td><td><button class='remCF1 btn btn-small btn-danger'" +
"onClick='delRow(" + i + ")' name='delButton'>Delete</button></td></tr>"
};
}
function SaveData(){
for (var i = 0; i < items.length; i++) {
document.getElementsByTagName("tbody")[0].innerHTML+= "<tr id='row" + i +"'><td>"+items[i].item+"</td><td>"+items[i].hsn+"</td><td>"+items[i].quantity+"</td><td>"+items[i].unit_name+"</td><td>"+items[i].unit_price+"</td><td>"+items[i].tax_code+"</td><td>"+items[i].amount+"</td></tr>"
};
}
console.log("OUT");
</script>
Views.py
if request.method == 'POST':
#SALES INVOICE
invoice_number=gen_invoice_number
invoice_date=request.POST['invoice_date']
customer_id=request.POST['customer_id']
customer_obj= Customer.objects.get(customer_id=customer_id)
special_instructions=request.POST['special_instructions']
company=company_label
#invoice total
total= 100 #request.POST['total']
CGST= 10 #request.POST['CGST']
SGST= 10 #request.POST['SGST']
discount=10 # request.POST['discount']
round_off= 100 #request.POST['round_off']
grand_total=100 #request.POST['grand_total']
#other details
user=username
print(user)
user_obj = User.objects.get(username=user)
print(user_obj)
remarks=request.POST['remarks']
sales_details=Sales_Invoice.objects.create(invoice_number=invoice_number,invoice_date=invoice_date,customer_id=customer_obj,company=company,special_instructions=special_instructions,total=total,CGST=CGST,SGST=SGST,discount=discount,round_off=round_off,grand_total=grand_total,user=user_obj,remarks=remarks)
sales_details.save()
total_sales = Sales_Invoice.objects.filter(company=company_label).all().count() + 1
new_gen_invoice_number=f"{company_label}-{month}{year}-{total_sales}"
#ITEM DETAILS
sales_invoice_id = gen_invoice_number
sales_invoice_id_obj=Sales_Invoice.objects.get(invoice_number=sales_invoice_id)
item = request.POST['item']
item_obj= New_Stock_Entry.objects.get(item=item)
hsn = request.POST['hsn']
quantity = request.POST['quantity']
tax_code = request.POST['tax_code']
unit_name = request.POST['unit_name']
unit_obj = Unit_Setting.objects.get(unit_name=unit_name)
unit_price = request.POST['unit_price']
amount = request.POST['amount']
items=Items_Details.objects.create(sales_invoice_id=sales_invoice_id_obj,item=item_obj,hsn=hsn,quantity=quantity,unit_name=unit_obj,unit_price=unit_price,tax_code=tax_code,amount=amount)
items.save()