[Answered ]-Conver Sub Query and CASE to Django ORM

2👍

Answer by myself.

from django.db.models import Case, When, Value, BooleanField

favorite_items = FavoriteItem.objects.filter(user_id='test_user')
items = Item.objects.annotate(
          is_favorite_item=Case(
            When(item_id__in=favorite_items.values('item_id'), then=True),
          default=Value(False),
          output_field=BooleanField()))
👤joseph

Leave a comment