title: How can I join two tables in Django and use the result in my template?
body: <p>I am having a hard time utilizing a foreign key in Django 1.7.  For reference:</p>
  
  <p><strong>models.py</strong> looks like this:</p>
  
  <pre><code>from django.db import models
  
  class Transaction(models.Model):
      transaction_num = models.IntegerField(primary_key=True)
      customer_name = models.CharField(max_length=255, blank=True)
      transaction_date = models.DateField(blank=True, null=True)
      phone = models.CharField(max_length=255, blank=True)
      est_pickup_date = models.DateField(blank=True, null=True)
  
      class Meta:
          db_table = 'transaction'
  
          def __str__(self):              # __unicode__ on Python 2
              return str(self.transaction_num)
  
  class Item(models.Model):
      itemid = models.IntegerField(primary_key=True)
      transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
      desc = models.CharField(max_length=255)
      picked_up_on = models.DateField(blank=True, null=True)
  
      class Meta:
          db_table = 'item'
  
      def __str__(self):              # __unicode__ on Python 2
          return str(self.itemid)
  
  class TransactionNote(models.Model):
      noteid = models.IntegerField(primary_key=True)
      transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
      content = models.CharField(max_length=255)
  
      class Meta:
          db_table = 'transaction_note'
  
      def __str__(self):              # __unicode__ on Python 2
          return str(self.noteid)
  </code></pre>
  
  <p><strong>views.py</strong> looks like this:</p>
  
  <pre><code>from django.http import HttpResponse
  from django.shortcuts import render
  from pickup.models import Transaction, Item, TransactionNote
  
  # Current Inventory
  def history(request):
      t = Transaction.objects.all().order_by('-transaction_date', '-transaction_num')
  
      return render(request, 'pickup/history.html', { 'transaction_list': t})
  </code></pre>
  
  <p>...and the relevant part of <strong>history.html</strong>, a table inside of a loop, where I want to include the count of all of the related items attached to a particular transaction:</p>
  
  <pre><code>{% for transaction in transaction_list %}
  
  [...]
  
  &lt;td&gt;{{ transaction.customer_name }}&lt;/td&gt;
  &lt;td&gt;{{ transaction.transaction_date }}&lt;/td&gt;
  &lt;td&gt;{{ transaction.est_pickup_date }}&lt;/td&gt;
  &lt;td&gt;{{ transaction.phone }}&lt;/td&gt;
  &lt;td&gt;{{ transaction.itemid.count }}&lt;/td&gt;
  
  [...]
  
  {% endfor %}
  </code></pre>
  
  <p>Everything works except for that last line.  I have no idea how to join the Transaction and Item tables so that I can work with the relevant item objects (specifically the descriptions or their counts) inside of the template.  Any help will be appreciated.</p>