假设我有 2 个模型客户和帐户。
楷模
class Account(models.Model):
customer = models.ForeignKey(Customer,on_delete=models.CASCADE,
blank=True,null=True,related_name='account')
desc = models.CharField(max_length=100)
paid = models.IntegerField(default=0)
received = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
class Customer(models.Model):
name = models.CharField(max_length=30,unique=True)
contact = models.CharField(max_length=10)
我想访问模板中收到的总和和已付字段的总和
客户视图
def show_customer(request,id=None):
customer = Customer.objects.filter(user=request.user)
return render(request,'customer/show_customer.html',{'customer':customer})
show_customer.html
<html>
{% for cust in customer %}
{{cust.name}}
{{cust.contact}}
**Here I want sum of paid & sum of receive for current customer**
</html>