Answer the question
In order to leave comments, you need to log in
How to sort parent objects by children?
There are 2 models:
class Chat(models.Model):
members = models.ManyToManyField(User, verbose_name="Участник", related_name='members')
class Message(models.Model):
author = models.ForeignKey(User, verbose_name="Отправитель", on_delete=models.CASCADE)
recipient = models.ForeignKey(Chat, related_name='received_messages', verbose_name="Получатель",
on_delete=models.CASCADE)
Answer the question
In order to leave comments, you need to log in
Add the time of sending to the message,
and as a result, the sorting will be like this:
Chat.objects.order_by('received_messages__timestamp')
If timestamp is a field in the Message of type models.DateTimeField
PS A minus at the beginning is possible
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question