Django pass value to subquery inside annotate in a query set with mptt
i have a class based on ListCreateAPIView where i'm modifing get_queryset method.. i need pass inside a subquery for an annotate a paramether but seem OutherRef dosen't work
i'm using Mptt library .... subqueryItem works fine, subqueryNested instead says "This queryset contains a reference to an outer query and may only be used in a subquery."
def get_queryset(self):
queryset = super().get_queryset()
subqueryItem = Subquery(Item.objects.filter(category=OuterRef('id')).order_by()
.values('category').annotate(count=Count('pk'))
.values('count'), output_field=IntegerField())
subqueryNested = Subquery(Item.objects.filter(category__in=ItemCategory.objects.get(pk=OuterRef('id')).get_descendants(include_self=True)).order_by()
.values('category').annotate(count=Count('pk'))
.values('count'), output_field=IntegerField())
queryset = queryset.annotate(
collcount = Coalesce(subqueryItem, 0),
collcountdeep = Coalesce(subqueryNested, 0),
)