Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:54:31+00:00 2026-06-01T07:54:31+00:00

I have an application where users select their own display columns. Each display column

  • 0

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.

The models are like (this is just an example model, actual has more than 100 fields):

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()

I tried doing:

ratios = {'growth':'quarter__sales_now / quarter__sales_previous'}
CompanyCode.objects.extra(select=ratios)
# raises "Unknown column 'quarter__sales_now' in 'field list'"

I also tried using raw query:

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

So please give me a little clue as to how I can use related columns preferably using ‘extra’ command. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:54:32+00:00Added an answer on June 1, 2026 at 7:54 am

    Ok, I found it out. In above using:

    CompanyCode.objects.select_related('quarter').extra(select=ratios)
    

    solved the problem.

    Basically, to access any related model data through ‘extra’, we just need to ensure that that model is joined in our query. Using select_related, the query automatically joins the mentioned models.

    Thanks :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have an application where users can store items. Now each type of account
I have a wxPython application which allows the users to select items from menus
I have a web application that has been running just fine in Internet Explorer
I have built a ruby on rails app that lets users track their workouts.
i have created an application where user can post message to their own wall
I am trying to provide drop down menus so that users can select their
I have a shared web server setup among friends. Each website has its own
I have a list of users on my php application (using codeigniter). Each user
I have a simple application in which I need to let the user select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.