#26426: Use case for QuerySet.extra: annotate query with the existence of a
relation
--------------------------------+--------------------------------------
     Reporter:  yourcelf        |                    Owner:  nobody
         Type:  Uncategorized   |                   Status:  new
    Component:  Uncategorized   |                  Version:  1.9
     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 shaib):

 I agree that these aren't very intuitive and are less efficient that the
 `extra` or `RawSQL`, but technically,

 Prefetch:
 {{{#!python
 curr_user_query = User.objects.filter(id=request.user.id)
 Category.objects.all().prefetch(Prefetch('followers',
                                          queryset=curr_user_query,
                                          to_attr=is_following))
 }}}
 I am also quite certain an annotate solution can be found involving a
 variation (which I don't particularly care to get completely right) on
 `Sum(Case(When(followers__id=Value(request.user.id), 1), default=0))`.

 But more generally -- this falls into a wide category of cases we should
 support, that of generalized joins (that is, joins whose conditions are
 specified by the user, and are not just along FK relationships).

 (I'm not sure there's a ticket for generalized joins, although it has been
 mentioned before; if there is, this should be marked duplicate, and if
 there isn't, it should be accepted).

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

Reply via email to