My models :
I have 2 (simplified) models:
class Message(models.Model):
mmsi = models.IntegerField()
time = models.DateTimeField()
class LastMessage(models.Model):
mmsi = models.IntegerField()
time = models.DateTimeField()
From the most recent Message, I want to check if there are Message not in LastMessage.
I can get the most recent Message like that :
recent_messages = (Message.objects.distinct('mmsi')
.order_by('mmsi', '-time'))
From there, I have no idea how to extract the informations I want from recent_message and LastMessage.
I'd typically like to have the information displayed like this :
MMSI | Message_Time | LastMessage_Time
I can do that with SQL like this for example :
SELECT r.mmsi, r.time as Message_Time, l.time as LastMessage_Time
FROM recent_message as r
INNER JOIN core_lastmessage as l on r.mmsi = l.mmsi
WHERE r.time <> l.time LIMIT 10;
┌─────────┬────────────────────────┬────────────────────────┐
│ mmsi │ message_time │ lastmessage_time │
├─────────┼────────────────────────┼────────────────────────┤
│ 2000000 │ 2019-09-10 10:42:03+02 │ 2019-09-10 10:26:26+02 │
│ 2278000 │ 2019-09-10 10:42:24+02 │ 2019-09-10 10:40:18+02 │
│ 2339002 │ 2019-09-10 10:42:06+02 │ 2019-09-10 10:33:02+02 │
│ 2339004 │ 2019-09-10 10:42:06+02 │ 2019-09-10 10:30:07+02 │
│ 2417806 │ 2019-09-10 10:39:19+02 │ 2019-09-10 10:37:02+02 │
│ 2417807 │ 2019-09-10 10:41:18+02 │ 2019-09-10 10:36:55+02 │
│ 2417808 │ 2019-09-10 10:42:23+02 │ 2019-09-10 10:30:39+02 │
│ 2470087 │ 2019-09-10 10:42:23+02 │ 2019-09-10 10:39:13+02 │
│ 3160184 │ 2019-09-10 10:42:03+02 │ 2019-09-10 10:28:30+02 │
│ 3604482 │ 2019-09-10 10:42:10+02 │ 2019-09-10 10:35:29+02 │
└─────────┴────────────────────────┴────────────────────────┘
(here recent_message is just a temporary table for conveniance)
How could I do that in Django ?
Thanks !