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

  • Home
  • SEARCH
  • 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 1063587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:48:18+00:00 2026-05-16T18:48:18+00:00

I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =

  • 0

I have the following model structure below:

class Master(models.Model): 
    name = models.CharField(max_length=50)          
    mounting_height = models.DecimalField(max_digits=10,decimal_places=2)   

class MLog(models.Model):                  
    date = models.DateField(db_index=True)
    time = models.TimeField(db_index=True)      
    sensor_reading = models.IntegerField()      
    m_master = models.ForeignKey(Master)

The goal is to produce a queryset that returns all the fields from MLog plus a calculated field (item_height) based on the related data in Master

using Django’s raw sql:

querySet = MLog.objects.raw('''
    SELECT a.id,
           date,
           time,
           sensor_reading,
           mounting_height,
          (sensor_reading - mounting_height) as item_height 
    FROM db_mlog a JOIN db_master b 
                     ON a.m_master_id = b.id
''')                                

How do I code this using Django’s ORM?

  • 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-05-16T18:48:18+00:00Added an answer on May 16, 2026 at 6:48 pm

    I can think of two ways to go about this without relying on raw(). The first is pretty much the same as what @tylerl suggested. Something like this:

    class Master(models.Model):
        name = models.CharField(max_length=50)
        mounting_height = models.DecimalField(max_digits=10,decimal_places=2)
    
    class MLog(models.Model):
        date = models.DateField(db_index=True)
        time = models.TimeField(db_index=True)
        sensor_reading = models.IntegerField()
        m_master = models.ForeignKey(Master)
    
        def _get_item_height(self):
            return self.sensor_reading - self.m_master.mounting_height
        item_height = property(_get_item_height)
    

    In this case I am defining a custom (derived) property for MLog called item_height. This property is calculated as the difference of the sensor_reading of an instance and the mounting_height of its related master instance. More on property here.

    You can then do something like this:

    In [4]: q = MLog.objects.all()
    
    In [5]: q[0]
    Out[5]: <MLog: 2010-09-11 8>
    
    In [6]: q[0].item_height
    Out[6]: Decimal('-2.00')
    

    The second way to do this is to use the extra() method and have the database do the calculation for you.

    In [14]: q = MLog.objects.select_related().extra(select = 
              {'item_height': 'sensor_reading - mounting_height'})
    
    In [16]: q[0]
    Out[16]: <MLog: 2010-09-11 8>
    
    In [17]: q[0].item_height
    Out[17]: Decimal('-2.00')
    

    You’ll note the use of select_related(). Without this the Master table will not be joined with the query and you will get an error.

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

Sidebar

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.