Django ORM equivalent for this SQL..calculated fie

2019-05-20 02:40发布

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?

2条回答
再贱就再见
2楼-- · 2019-05-20 03:18

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.

查看更多
闹够了就滚
3楼-- · 2019-05-20 03:26

I always do the calculations in the app rather than in the DB.

class Thing(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField()     
    @Property
    def diff():
        def fget(self):
            return self.foo - self.bar
        def fset(self,value):
            self.bar = self.foo - value

Then you can manipulate it just as you would any other field, and it does whatever you defined with the underlying data. For example:

obj = Thing.objects.all()[0]
print(obj.diff)  # prints .foo - .bar
obj.diff = 4     # sets .bar to .foo - 4

Property, by the way, is just a standard property decorator, in this case coded as follows (I don't remember where it came from):

def Property(function):
    keys = 'fget', 'fset', 'fdel'
    func_locals = {'doc':function.__doc__}
    def probeFunc(frame, event, arg):
        if event == 'return':
            locals = frame.f_locals
            func_locals.update(dict((k,locals.get(k)) for k in keys))
            sys.settrace(None)
        return probeFunc
    sys.settrace(probeFunc)
    function()
    return property(**func_locals)
查看更多
登录 后发表回答