How to perform Rank query in Django

I am stuck at this point and this query seems a bit tricky to me so basically i am getting data from devices in periodic intervals 'lets say 3 hours'

But every device is sending data twice on every interval(3 hours), i am required to extract data that is send last(in intervals)

"date": "2021-11-28",
        "time": "12 : 35",
        "meter": "70F8E7FFFE10C495",
       
        "dr": 3,
        "date_received": "2021-11-28T18:05:20.473430+05:30",
       
    },
    {
        "id": 2,
        "date": "2021-11-28",
        "time": "12 : 37",
        "meter": "70F8E7FFFE10C459",
       
        "date_received": "2021-11-28T18:07:09.980403+05:30",
   
    },
    {
        "id": 3,
        "date": "2021-11-28",
        "time": "12 : 37",
        "meter": "70F8E7FFFE10C459",
       
        "dr": 3,
        "date_received": "2021-11-28T18:07:11.533388+05:30",
      
    },
    {
        "id": 4,
        "date": "2021-11-28",
        "time": "12 : 50",
        "meter": "70F8E7FFFE10C463",
       
        "date_received": "2021-11-28T18:20:44.197284+05:30",
       
    },
    {
        "id": 5,
        "date": "2021-11-28",
        "time": "12 : 56",
        "meter": "70F8E7FFFE10C47D",
     
        "date_received": "2021-11-28T18:26:43.221316+05:30",
      
    },
    {
        "id": 6,
        "date": "2021-11-28",
        "time": "12 : 56",
        "meter": "70F8E7FFFE10C47D",   <---- only want to get this occurrence 
      
        "date_received": "2021-11-28T18:26:44.925292+05:30",
     
    },

So if device : 70F8E7FFFE10C459 sends data twice i want to retrieve only last sent data , and this needs obj in queryset , i was told about RANK concept don't understand how to apply that here

class MeterData(models.Model):
    meter = models.ForeignKey('dashboard.Meter',on_delete=models.SET_NULL,null=True)
    customer = models.ForeignKey('Customer',on_delete=models.SET_NULL,null=True,blank=True)
    battery  = models.CharField(max_length=40,null=True,blank=True)
    crc_status = models.CharField(max_length=43,null=True,blank=True)
    Pulse_count = models.IntegerField(null=True,blank=False)
    status_data = models.CharField(max_length=50,blank=True,null=True)
    status_magnet_temper = models.BooleanField(default=False)
    meter_reading = models.CharField(max_length=50,null=True,blank=True)
    status_switch_temper = models.BooleanField(default=False)
    status_voltage_level = models.CharField(max_length=10,null=True,blank=True)
    uplink_type = models.CharField(max_length=50,null=True,blank=True)
    confirmed_uplink = models.BooleanField(max_length=50,null=True,blank=True)
    tx_info_frequency = models.CharField(max_length=50,null=True,blank=True)
    dr = models.IntegerField(null=True,blank=True)
    date_received = models.DateTimeField(auto_now_add=True,null=True)

It doesn't work on Sqlite

I think the following query is the correct answer.

MeterData.objects.filter(put_your_filters_here).order_by('-date_received').distinct('customer_id')

if it doesn't work let me know.

Back to Top