I’m having a trouble on how can I automatically update my amount_unpaid
when the time I updated my amount_paid
column. The result of amount_unpaid should be subtract my amount
– amount_paid
= amount_unpaid
. The way I updated my data is using list like the code below, please check this out, Any help is much appreciated Thanks in advance.
AttributeError: ‘QuerySet’ object has no attribute ‘amount’
JavaScript
x
9
1
def update_remarks(request):
2
product_ids = request.POST.getlist('id[]')
3
amt_paid = request.POST.get('amount_paid')
4
5
for id in product_ids:
6
unpaid_update = (Person.objects.filter(pk=id).amount - amt_paid) #Error no attribute Amount
7
Person.objects.filter(pk=id).update(amount_paid = unpaid_update) # this should update every amount_unpaid based on subtracted amount column
8
Person.objects.filter(pk=id).update(amount_paid = amt_paid)
9
Advertisement
Answer
I think the minimalist solution is:
JavaScript
1
7
1
from django.db.models import F
2
3
def update_remarks(request):
4
product_ids = request.POST.getlist('id[]')
5
amt_paid = int(request.POST.get('amount_paid', 0))
6
Person.objects.filter(pk__in=product_ids).update(amount_paid=amt_paid, amount_unpaid=F('amount')-amt_paid)
7
I am using F(<field_name>)
to get the value of the field from that object. Hence I do not need a for loop.
For more information, please see the documentation on update(...)
and F()
.