A
A
Arsen Abakarov2017-05-13 12:36:18
PostgreSQL
Arsen Abakarov, 2017-05-13 12:36:18

Django subquery?

I want to make a request with a subquery using Django ORM, like

SELECT log_out.id, log_out.site_id, log_out.date
FROM (
    SELECT site_id, max(date) AS max_date
    FROM main_uploadedprofilelog
    GROUP BY site_id
) AS log_in 
INNER JOIN main_uploadedprofilelog AS log_out ON log_out.site_id = log_in.site_id AND log_out.date = log_in.max_date;

It was not smart enough to do everything in one operator .. (I want to use extra last)
I decided to make a subquery first
items = UploadedProfileLog.objects.values('site_id').annotate(Max('date'))

How to shove this ValueQuerySet into the filter of the main request now?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question