Answer the question
In order to leave comments, you need to log in
Can an annotated field be assigned not a simple value (str, int, False, ...), but a model object?
class Chat(models.Model):
def last_message(self):
return self.messages.last()
class Message(models.Model):
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='messages')
text = models.TextField()
Chat.objects.prefetch_related(Prefetch('messages', queryset=last_messages)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question