我正在使用django-taggit。 我正在标记操作项和信息项。 我想列出所有有信息项的标签。 我最初写道:
Tag.objects.filter(info_item__name__isnull=False).annotate(info_item_count=Count('info_item'))
但这返回了一些操作项。 如何重写查询,以便过滤标记,以便附加信息项?
假设您的info_item模型是 InfoItem,然后尝试
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'))