Хорошо, я отвечу на это, предполагая, что в модели Letter
есть ForeignKey
, указывающий на модель MailBox
.
Для этого вы можете использовать Подзапрос :
# I'm assuming here a couple of things:
# The fields content and date_created exist in your Letter model,
# if there is no date created field, use instead whatever you are using for
# establish some sort of order creation.
MailBox.objects.annotate(
last_letter_content = Subquery(
Letter.objects.filter(mailbox=OuterRef('pk')).order_by('-date_created').values('content')[:1]
)
)