title: "SELECT...AS..." with related model data in Django
body: <p>I have an application where users select their own display columns. Each display column has a specified formula. To compute that formula, I need to join few related columns (one-to-one relationship) and compute the value.</p>
  
  <p>The models are like (this is just an example model, actual has more than 100 fields):</p>
  
  <pre><code>class CompanyCode(models.Model):
      """Various Company Codes"""
  
      nse_code = models.CharField(max_length=20)
      bse_code = models.CharField(max_length=20)
      isin_code = models.CharField(max_length=20)    
  
  class Quarter(models.Model):
      """Company Quarterly Result Figures"""
  
      company_code = models.OneToOneField(CompanyCode)
      sales_now = models.IntegerField()
      sales_previous = models.IntegerField()
  </code></pre>
  
  <p>I tried doing:</p>
  
  <pre><code>ratios = {'growth':'quarter__sales_now / quarter__sales_previous'}
  CompanyCode.objects.extra(select=ratios)
  # raises "Unknown column 'quarter__sales_now' in 'field list'"
  </code></pre>
  
  <p>I also tried using raw query:</p>
  
  <pre><code>query = ','.join(['round((%s),2) AS %s' % (formula, ratio_name)
      for ratio_name, formula in ratios.iteritems()])
  companies = CompanyCode.objects.raw("""
      SELECT `backend_companycode`.`id`, %s
      FROM  `backend_companycode` 
      INNER JOIN  `backend_quarter` ON (  `backend_companycode`.`id` =  `backend_companyquarter`.`company_code_id` ) 
      """, [query])
  #This just gives empty result
  </code></pre>
  
  <p>So please give me a little clue as to how I can use related columns preferably using 'extra' command. Thanks.</p>