﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23854	Add custom SelectRelated classes	Anssi Kääriäinen	nobody	"The idea is similar to the Prefetch object. One could for example do:
{{{
    qs = Post.objects.select_related(latest_comment=Latest('comments', by='post_date'))
}}}
where the models are Post, and a list of comments to that post with related_name='comments'. Django would then generate a query to fetch the latest comment for each post. The SQL generated could be for example:
{{{
SELECT post.*, latest_comment.*
  FROM post
  LEFT JOIN (select author.*
               from author
              where author.post_date = (select max(post_date) from author inner_author where inner_author.post_id = author.post_id
            ) latest_comment ON latest_comment.post_id = post.id
}}}

It should also be possible to do `qs.order_by('latest_comment__post_date')` and other query operations on the select_related ""annotation"".

To implement this the following things are needed:
  - necessary alterations to select_related() call
  - a way to do custom subselect joins (see #23853)
  - addition of ""model annotations"" to the query (so that resolve_ref() will know to resolve
    latest_comment to the custom select_related)
  - substantial changes to the way select_related() resolvation works in compiler and QuerySet classes"	New feature	new	Database layer (models, ORM)	dev	Normal			Simon Charette	Accepted	0	0	0	0	0	0
