Django ORM GROUP BY

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)

+3
source share
2 answers

Here is an overview of aggregation execution in Django ORM , namely values.

UPDATE: To put it into action, something similar to

Invitation.objects.values('to_email').annotate(date_invited=Max('date_invited'))

should work for your example.

2: , to_email date_invited . , , , " " Python. , ( , , ).

+3

, ORM Django . ORM . SQL DISTINCT python.

Edit

, Hank Gay, , date_invited to_email, - .

+1

Source: https://habr.com/ru/post/1714498/


All Articles