This PostgreSQL query solves my problem, but I'm not sure how to put it in Django ORM.
SELECT DISTINCT ON (to_email) * FROM (SELECT * FROM invitation_invitation ORDER BY date_invited DESC) AS sub
From the list of elements I want all the different rows for the column "to_email" and where to_email are duplicates, it should select the last one (date_invited)
source
share