1
I did it like this:
def tags(request):
published_tags = Tag.objects\
.filter(is_published=True, posts__is_published=True)\
.exclude(posts=None)\
.order_by('name')\
.distinct()
return {'tags': published_tags}
Source:stackexchange.com