1
我有2個模型。 serviceinvoice和invoiceitems。 invoiceitems模型通過外鍵鏈接到serviceinvoice。Django:如何獲得有關單行的所有行的總和
現在對於單張發票,我如何獲得總折扣金額? 註釋不起作用,因爲它僅適用於單行。 Models.py
class serviceinvoice(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL,related_name='invoice')
invoice_number=models.CharField(max_length=100)
invoice_date = models.DateField()
class serviceinvoiceitems(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL,related_name='serviceinvoiceitem')
company=models.ForeignKey(Organisation,related_name='sitemcomp')
invoice_number=models.ForeignKey(serviceinvoice,related_name='serviceitems1')
discount = models.BooleanField(default=False)
discount_amount = models.FloatField(null=True,blank=True,default=0)
Amount=models.FloatField()
discription=models.CharField(max_length=500,blank=True,null=True)
我試圖在視圖:
invoice_detailmain = serviceinvoice.objects.get(pk=pk)
dis_value_total = Sum('invoice_detailmain__serviceitems1__discount_amount')
它不給DISCOUNT_AMOUNT的總和。 什麼是解決方案以及如何在模板中顯示它?