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?