[Answered ]-Exclude user from an annotation query

1👍

You filter this out in the aggregate:

from django.contrib.postgres.aggregates import ArrayAgg
from django.db.models import OuterRef, Q, Subquery

products = products.annotate(
    conversation_users=Subquery(
        Conversation.objects.filter(
            product=OuterRef('pk'),
        )
        .annotate(users_array=ArrayAgg('users__username', filter=~Q(users=user)))
        .values_list('users_array', flat=True)[:1]
    )
)

Leave a comment