Django, group by one field, only take the latest / max of each group and get back the ORM objects - SemicolonWorld

PHOTO EMBED

Fri Jan 14 2022 19:12:43 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol

Portfolio.objects.order_by().order_by(
    'code', # first, cause we want to group by this value
    '-created' # descending order, latest / max will be first
).distinct('code')
content_copyCOPY

https://www.semicolonworld.com/question/61934/django-group-by-one-field-only-take-the-latest-max-of-each-group-and-get-back-the-orm-objects