#32879: Using extra instead annotate in ORM while using select_for_update
-------------------------------------+-------------------------------------
     Reporter:  Zerq                 |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  closed
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  duplicate
     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 Zerq):

 For anyone who have the same problem:

 {{{
 sub =
 
Subquery(Bar.objects.filter(pk=OuterRef('pk')).annotate(count=Count('*')).values('count'))
 sub = Coalesce(sub, 0)
 query = Foo.objects.annotate(times_used=sub)
 }}}

 Small warning: If subquery has no values, it will return None, this is the
 reason for Coalesce.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32879#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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.911033f6085b13fa8f1f7a7a782833c1%40djangoproject.com.

Reply via email to