问题
- 是否可以使用Django 的聚合功能来计算 sumproduct?
背景
我正在为一张发票建模,它可以包含多个项目。Invoice 和 Item 模型之间的多对多关系是通过 InvoiceItem 中间表处理的。
发票的总金额(<code>amount_invoiced)是通过对给定发票上每个项目的乘积求和来计算unit_price
的quantity
。下面是我目前用来完成此任务的代码,但我想知道是否有更好的方法来使用Django 的聚合功能来处理此问题。
当前代码
class Item(models.Model):
item_num = models.SlugField(unique=True)
description = models.CharField(blank=True, max_length=100)
class InvoiceItem(models.Model):
item = models.ForeignKey(Item)
invoice = models.ForeignKey('Invoice')
unit_price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.DecimalField(max_digits=10, decimal_places=4)
class Invoice(models.Model):
invoice_num = models.SlugField(max_length=25)
invoice_items = models.ManyToManyField(Item,through='InvoiceItem')
def _get_amount_invoiced(self):
invoice_items = self.invoiceitem_set.all()
amount_invoiced = 0
for invoice_item in invoice_items:
amount_invoiced += (invoice_item.unit_price *
invoice_item.quantity)
return amount_invoiced
amount_invoiced = property(_get_amount_invoiced)