title: A simple database inner join using django
body: <p>I have two tables from a legacy database imported to my django app using inspectdb</p>
  
  <p>Here is my model definition</p>
  
  <pre><code>class AppCosts(models.Model):
      cost = models.DecimalField(max_digits=10, decimal_places=2)
  
  
  class AppDefinitions(models.Model):
      data = models.TextField()
      permissions = models.CharField(max_length=50, blank=True)
      appcost=models.OneToOneField(AppCosts, db_column='id')
  </code></pre>
  
  <p>Every app in AppDefinitions has one entry in AppCosts - a one to one relationship</p>
  
  <p>When I use select_related() I get :</p>
  
  <pre><code>&gt;&gt;&gt; AppDefinitions.objects.select_related().query.__str__()
  
      u'SELECT _app_definitions.id, _app_definitions.data,
    _app_definitions.permissions, _app_definitions.id, _app_costs.id, 
    _app_costs.cost FROM _app_definitions INNER JOIN _app_costs 
    ON ( _app_definitions.id = _app_costs.id )'
  
  &gt;&gt;&gt; a = AppDefinitions.objects.select_related().first()
  
  &gt;&gt;&gt; a.id
  u'abaqus'
  
  &gt;&gt;&gt; a.cost
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  AttributeError: 'AppDefinitions' object has no attribute 'cost'
  
  &gt;&gt;&gt; a.appcost
  &lt;AppCosts: abaqus,1.50&gt;    
  
  &gt;&gt;&gt; a.appcost.cost
  Decimal('1.50')
  </code></pre>
  
  <p>Now there are two problems :</p>
  
  <ul>
  <li>The query pulls in id twice from both fields (not a big deal because this table wont have more than a few hundred entries at most, but still, I want it to be right)</li>
  <li>I have to access cost as x.appcost.cost, rather than simply x.cost</li>
  </ul>
  
  <p>How do I achieve this?</p>
  
  <p>I would be loath to use custom SQL, because that would defeat the purpose of using django's ORM in the first place. </p>