Use annotate. https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Kenny Loveall Sent: Tuesday, April 23, 2019 12:58 PM To: django-users@googlegroups.com Subject: Is it possible to value based on another aggregation? Hi everyone, I'm trying to figure out if it's possible to get a value for one field based on the aggregation of another? Specifically, I've got a model with a created_at timestamp, a foreign key, and a created_by user foreign key. I want to group the objects based on the foreign key and get the time and user of the most recently created object. I know how to do this with a subquery and how to get the most recently created timestamp for each group of objects without a subquery, but I'm not sure how to get the user without using a subquery. Is this possible? Thanks, Kenny -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com<mailto:django-users@googlegroups.com>. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGABHYC3S-9o_ay1%2BPiANm6vv5py7tFBKyuUCzT1%2Bz49Z%2BpAnA%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAGABHYC3S-9o_ay1%2BPiANm6vv5py7tFBKyuUCzT1%2Bz49Z%2BpAnA%40mail.gmail.com?utm_medium=email&utm_source=footer>. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1b62c769d1248a283e4bae3c904a5db%40iss2.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.