Is it not possible to exclude inside a queryset? I always receive this error:
ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.
The part of the code I am wondering why is it not working:
def get_queryset(self):
return self.queryset.annotate(
foo_count=Subquery(
Foo.objects.exclude(code__hash=OuterRef('hash'))
.values('code')
.annotate(cnt=Count('pk'))
.values('cnt'),
output_field=IntegerField()
),
)
Just found out that it is not possible to
.excludeor negate~Q()withOuterRef()for that Django version.Found the similiar issue here: https://code.djangoproject.com/ticket/30739
The workaround is to get RAW SQL Query from
self.queryset.annotateand reformat it in the way you need.Here is an example of a solution that worked for me: