I want to convert the model instances into a JSON object and then pass it to the HTML file. However, I get an AttributeError at this line:
data = serializers.serialize("json", Inventory.objects.in_bulk())
The full views.py:
def add_invoice(request):
form = InvoiceForm(request.POST or None)
data = serializers.serialize("json", Inventory.objects.in_bulk())
total_invoices = Invoice.objects.count()
queryset = Invoice.objects.order_by('-invoice_date')[:6]
if form.is_valid():
form.save()
messages.success(request, 'Successfully Saved')
return redirect('/invoice/list_invoice')
context = {
"form": form,
"title": "New Invoice",
"total_invoices": total_invoices,
"queryset": queryset,
"data": data,
}
return render(request, "entry.html", context)
The Javascript code:
{{ data|json_script:"hello-data" }}
<script type="text/javascript">
const data = JSON.parse(document.getElementById('hello-data').textContent);
document.getElementById('id_line_one').onchange = function(event){
document.getElementById('id_line_one_unit_price').value = data[event.target.value].fields.amount;
};
</script>
models.py:
class Inventory(models.Model):
product_number = models.IntegerField(primary_key=True)
product = models.TextField(max_length=3000, default='', blank=True, null=True)
title = models.CharField('Title', max_length=120, default='', blank=True, unique=True)
amount = models.IntegerField('Unit Price', default=0, blank=True, null=True)
def __str__(self):
return self.title
How can I pass that JSON object and assign the amount value to the text field according to the object selected in the dropdown? Thanks

EDIT: Full Trace
AttributeError at /invoice/add_invoice/ 'int' object has no attribute '_meta' Request Method: GET Request URL: http://127.0.0.1:8000/invoice/add_invoice/ Django Version: 4.0 Exception Type: AttributeError Exception Value:
'int' object has no attribute '_meta' Exception Location: C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\serializers\base.py, line 110, in serialize Python Executable: C:\Users\Kiran\AppData\Local\Programs\Python\Python310\python.exe Python Version: 3.10.0 Python Path:
['C:\Users\Kiran\Desktop\src', 'C:\Users\Kiran\AppData\Local\Programs\Python\Python310\python310.zip', 'C:\Users\Kiran\AppData\Local\Programs\Python\Python310\DLLs', 'C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib', 'C:\Users\Kiran\AppData\Local\Programs\Python\Python310', 'C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages'] Server time: Sun, 10 Jul 2022 13:45:15 +0000 Traceback Switch to copy-and-paste view C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py, line 47, in inner response = get_response(request) … Local vars C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) … Local vars C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\decorators.py, line 21, in wrapped_view return view_func(request, *args, **kwargs) … Local vars C:\Users\Kiran\Desktop\src\invoicecemgmt\views.py, line 39, in add_invoice data = serializers.serialize("json", Inventory.objects.in_bulk()) … Local vars C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\serializers_init.py, line 129, in serialize s.serialize(queryset, **options) … Local vars C:\Users\Kiran\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\serializers\base.py, line 110, in serialize concrete_model = obj._meta.concrete_model … Local vars
EDIT: Javascript code-
var data = JSON.parse(document.getElementById('hello-data').textContent);
document.getElementById('id_line_one').onchange = function(event){
var data1 = data.find(({pk}) => pk === event.target.value);
alert(data1)
document.getElementById('id_line_one_unit_price').value = data1 && data1.amount ? data1.amount : 0;
};
If I type some primary key in the data.find()=>1== then the output works fine. I want to know how to send the primary key of all the objects in the data.find(()) so that I can find a match with the title selected and retrieve it's amount.
While testing that function with numerical values, I noticed that the site wasn't dynamical? Even though I changed the primary key in the code, It still displayed the amount of the previous primary key and it didn't update until and unless I deleted the product from the Inventory.