In Wagtail I want to get a list of Tags, each with a count of the number of Pages tagged, but only counting live()
pages.
Let's say I have this set-up, from the docs:
from django.db import models
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey
from taggit.models import TagBase, ItemBase
class BlogTag(TagBase):
class Meta:
verbose_name = "blog tag"
verbose_name_plural = "blog tags"
class TaggedBlog(ItemBase):
tag = models.ForeignKey(
BlogTag, related_name="tagged_blogs", on_delete=models.CASCADE
)
content_object = ParentalKey(
to='demo.BlogPage',
on_delete=models.CASCADE,
related_name='tagged_items'
)
class BlogPage(Page):
...
tags = ClusterTaggableManager(through='demo.TaggedBlog', blank=True)
I can get a list of BlogTag
s, each with a count
of the number of BlogPage
s tagged with it, by doing this (I think that's what it's doing...?):
from django.db.models.aggregates import Count
BlogTag.objects.annotate(count=Count("tagged_blogs"))
But I can't get my head round how to filter this to only count pages that are live()
(or that have some other Page-related quality).