2012-04-06 82 views
1

我正在使用django-taggit。我正在标记行动项目和信息项目。我想列出所有有信息项的标签。我原本写道:Django过滤器包含所有存在某种类型项目的标签

Tag.objects.filter(info_item__name__isnull=False).annotate(info_item_count=Count('info_item')) 

但是这是返回一些行动项目。我该如何重写查询,以便过滤标签以便附加信息项?

回答

1

假设你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'))