3👍
You have to explicitely cast preferences.a_id
to an explicit type, because it is a jsonb
on PostgreSQL not an int
solution
from django.db import models
from django.db.models.functions.comparison import Cast
A.objects.annotate(count=Count(B.objects.annotate(casted_id=Cast("preferences__a_id", models.IntegerField())).filter(casted_id=OuterRef('id'))))
used Django 4.1
Source:stackexchange.com