title: Django - show sums of a table of records
body: <p>I have a table which has a list of invoices and their details:</p>
  
  <pre><code>class Invoice(models.Model):
      invoiceNum = models.CharField(etc...)
      invoiceDate = models.DateField(etc...)
      customerID = models.ForeignKey(etc...)
      isPaid = models.CharField(etc...)
  </code></pre>
  
  <p>The <code>Invoice</code> records do not hold the actual invoice total. Instead, an invoice's total is made up of several <code>Invoice_Line_Item</code> records, held in another table:</p>
  
  <pre><code>class Invoice_Line_Item(models.Model):
      invNum = models.ForeignKey(Invoice)
      itemName = models.CharField(etc...)
      itemPrice = models.DecimalField(etc...)
  </code></pre>
  
  <p>I have a webapp that shows all the invoices in a big HTML table, along with all the details of that invoice on the table's <code>tr</code> row. Details such as, Invoice Date, Invoice Number, Customer ID, all come from that <code>Invoice</code> table. There are hundreds of invoices to display in this <code>HTML</code> <code>table</code>.</p>
  
  <p>What I would like to do is also show each invoice's total value - which is the sum of all the line items. However, I can't think of a simple way to acomplish this since the invoice details and the line items that make up the invoice's total are in two different tables.</p>
  
  <p>One way I thought is to pass the entire <code>Invoice_Line_Item</code> querySet into the HTML template, then for each invoice displayed in a table <code>tr</code>, I could iterate over the entire <code>Invoice_Line_Item</code> querySet, adding up all the line items that match the current invoice. This, however, seems hugely inefficient. </p>
  
  <p>Any better ideas on how to do this?</p>
  
  <p>Thanks!</p>