1👍
✅
You want to work with an Exists
subquery [Django-doc] to prevent joining:
from django.db.models import Exists, OuterRef
def get(self, request, **kwargs):
user = request.user
companies = Company.objects.annotate(
joined=Exists(
Queue.objects.filter(company_id=OuterRef('pk'), users=request.user)
)
)
# …
For the serializer, you add a joined
field:
class CompanySerializer(serializers.ModelSerializer):
joined = serializers.BooleanField()
# …
Source:stackexchange.com