#23854: Add custom SelectRelated classes
-------------------------------------+-------------------------------------
Reporter: akaariai | Owner: nobody
Type: New | Status: new
feature | Version: master
Component: Database | Keywords:
layer (models, ORM) | Has patch: 0
Severity: Normal | Needs tests: 0
Triage Stage: | Easy pickings: 0
Unreviewed |
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/23854>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.f9053ab330f00b5b173bfd195049fea0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.