如何查询以获取thread
中包含最新消息的order
。
修改
例如:
models.py:
class Thread(models.Model):
user = models.ManyToManyField(User)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sent_date = models.DateTimeField(default=datetime.now)
body = models.TextField()
user = models.ForeignKey(User)
答案 0 :(得分:2)
试试这个:
from django.db.models import Max
Thread.objects.annotate(max_sent_date=Max('message_set__sent_date')).order_by('-max_sent_date')