I'm using django-taggit. I am tagging action items and info items. I want to list all tags for which there is an info item. I originally wrote:
Tag.objects.filter(info_item__name__isnull=False).annotate(info_item_count=Count('info_item'))
But this is returning some action items. How might I rewrite the query so that the Tags are filtered such that there is an info item attached?
Assuming your info_item model is InfoItem, then try
from django.contrib.contenttypes.models import ContentType
Tag.objects.filter(
taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(InfoItem),
info_item__name__isnull=False).annotate(info_item_count=Count('info_item'))