Добрый день! Не так давно опять сел осваивать Django, столкнулся с orm и немного... Задумался. Есть простенькая модель
from django.conf import settings
from django.db import models
class Message(models.Model):
message_num = models.IntegerField()
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
name = models.CharField(max_length=50)
creation_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return ''.join([
str(self.id),
' ',
str(self.message_num),
' ',
self.name
])
class MessageVersion(models.Model):
message_id = models.ForeignKey(Message)
version_num = models.PositiveIntegerField()
text = models.TextField()
creation_time = models.DateTimeField(auto_now_add=True)
select res1.*, res2.* from homepage_messageversion res1
join (
select message_id_id, max(version_num) as version_num
from homepage_messageversion
group by message_id_id
) res2
on (res1.message_id_id = res2.message_id_id)
and (res1.version_num = res2.version_num);