SHARE
TWEET

Untitled

a guest Jun 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT COUNT("messages_message"."id")
  2. FROM "messages_message"
  3. WHERE "messages_message"."ignored" = FALSE
  4.   AND (
  5.     "messages_message"."sender_id" = "users_user"."id"
  6.     OR
  7.     "messages_message"."receiver_id" = "users_user"."id"
  8.   )
  9.      
  10. from django.db.models import Count, OuterRef, Subquery, Q
  11.  
  12. User.objects.annotate(
  13.     msgs_count=Count(Subquery(
  14.         Message.objects.filter(
  15.             Q(sender_id=OuterRef('pk')) | Q(receiver_id=OuterRef('pk')),
  16.             ignored=False
  17.         )
  18.     ))
  19. )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top