#25403: A quite common use of .extra()
-------------------------------------+-------------------------------------
     Reporter:  PaoloC68             |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by akaariai):

 I am convinced having something like
 {{{
 Post.objects.select_related(
     latest_comment=Select(
         'comments', queryset=Comments.objects.order_by('-created_at')
     )
 )
 }}}
 in Django is a great idea.

 If we got .filter() to accept expressions, then you could do exactly the
 same raw SQL query with expressions as the blog mentions. That is,
 `extra(where=['(blog_id, added) IN %s' % (values,)])` ==
 `.filter(RawSQL('(blog_id, added) IN %s', (values,))`.

 Note that the .extra() version used in the blog post uses string
 formatting. While as used in the blog post the query is safe, you might be
 vulnerable to SQL injection attacks if you use the same pattern with user
 editable char fields for example.

--
Ticket URL: <https://code.djangoproject.com/ticket/25403#comment:4>
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/066.5218cf5c68227f028c40d04819d10d30%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to