title: Django three way join using Foreign Keys
body: <p>I recently started evaluating Django for migrating our archaic web application written 10 years ago. I have been reading up Django documentation for the last few days, but haven't been able to figure out the best way to achieve a multi table database join in my case:</p>
  
  <pre><code>Model:
  
  class Product(models.Model):
      productid = models.IntegerField(primary_key=True, db_column='ProductId') 
      productname = models.CharField(max_length=120, db_column='ProductName')
  
  class Testcases(models.Model):
      testcaseid = models.IntegerField(primary_key=True, db_column='TestCaseId')
      testcasename = models.CharField(max_length=240, db_column='TestCaseName')
  
  class Testmatrix(models.Model):
      testmatrixid = models.IntegerField(primary_key=True, db_column='TestMatrixId')
      productid = models.ForeignKey(Product, db_column='ProductId')
      testcaseid = models.ForeignKey(Testcases, db_column='TestCaseId')
  
  class Status(models.Model):
      testmatrixid = models.ForeignKey(Testmatrix, db_column='TestMatrixId')
      title = models.CharField(max_length=240, db_column='Title', blank=True)
  </code></pre>
  
  <p><i>(Note that model was generated by inspectdb and I'd prefer not to modify it at this point in time)</i></p>
  
  <pre><code>View:
  
  from django.shortcuts import render_to_response
  from mysite.testmatrix.models import Product, Testcases, Testmatrix, Status
  
  def get_products(request):
      tm = list(Testmatrix.objects.filter(productid='abc'))
      return render_to_response('products.html', {'tm': tm})
  </code></pre>
  
  <p>template is designed to be minimal at this point to help focus on the real issue in (views/model).</p>
  
  <pre><code>Template: (products.html)
  {% extends "main.html" %}
  
  {% block body %}
  &lt;table&gt;
  {% for tm in tm %}
      &lt;tr&gt;
              &lt;td&gt;{{ tm.testmatrixid }}&lt;/td&gt;
      &lt;td&gt;{{ tm.testcaseid.testcasename }}&lt;/td&gt;
      &lt;/tr&gt;
  {% endfor %}
  &lt;/table&gt;
  {% endblock %}
  </code></pre>
  
  <p><b>Problem:</p>
  
  <p>Although I'm able to join Testmatrix and Testcase models, I am unable to generate an equivalent queryset by joining all of TestMatrix, TestCase, Status records on say productid='abc' </b></p>
  
  <p>I tried the following:</p>
  
  <p>1) Use select_related between Testmatrix and Testcases and Product tables and was able to access attributes across all three models (testmatrixid, productid, productname, testcaseid, testcasename). However I'm not sure how to extend this auto foreign key referencing to Status model. This would have been easier if all Foreign Keys were defined within Testmatrix itself. But Status has a Foreign Key to TestMatrix.</p>
  
  <p>2) I tried using something like: entries = Status.objects.filter(testmatrixid__productid=pid). This again gave me a queryset as a result of joining Testmatrix and Status, but not Testcases.</p>
  
  <p>Pardon any blaring mistakes or bloopers. This is my very first post!</p>