Annotations Force Cached Queryset Evaluation

I am having issues using cache with annotate. When I run annotate, the queryset is evaluated, ignoring the cached results. This makes it much slower. I’d like to be able to annotate my model without having to fully run the query. Is this possible? If not, why not?

from django.core.cache import cache
from django.db.models import Exists, OuterRef

favorites = Favorites.objects.filter(item = OuterRef(‘pk’), user=user)
best_items = cache.get(‘best_items’)
best_items = best_items.annotate(is_favorite= Exists(favorites))

i dont have enough reputation to comment,would you please share your models.py

Back to Top