Skip to main content
 首页 » 编程设计

python之获取标签文章数

2025年05月04日12haluo1

我想从 taggitItem 表中获取每个标签的文章数; 我写了以下内容,但它不能正常工作。

article_content_type = ContentType.objects.get_for_model(Article) 
articles = TaggedItem.objects.filter(content_type = article_content_type).annotate(count = Count('tag')) 

请帮忙。

请您参考如下方法:

article_count = TaggedItem.objects.filter(content_type = article_content_type).count() 

也许最好不要使用 TaggedItem 类,而是通过 Article 类来实现。