Search code examples
pythondjangotagsdjango-taggit

Django-Taggit | Getting ALL tags, but only related to blog


I am using django-taggit to tag three different types of models:

  • a blog
  • a list of events
  • a list of products.

This all works fine.

I am now trying to grab a list of all the available tags related to the blog so I can display them on the BlogIndex page. The furthest I've gotten is, thanks to a similar SO question:

from taggit.models import Tag

def blog_tags(self):
    tags = Tag.objects.all()
    return tags

However this gives me a list of every single tag, including the product and event tags.

How do I filter that list of tags down to just the blog?

EDIT | modely.py simplified:

class BlogPostTag(TaggedItemBase):
    content_object = ParentalKey('BlogPost', related_name='tagged_items')

class BlogPost(Page):
    # my fields
    tags = ClusterTaggableManager(through=BlogPostTag, blank=True)

    # Getting BlogPost-specific tags here is not difficult

class BlogIndex(Page):
    # my fields

    def blog_tags(self):
        etc...

Solution

  • For anyone else looking for an answer to this question, I found a simple workaround:

    def blog_tags(self):
        tags = BlogPost.tags.most_common()
        return tags
    

    This lists all the tags associated with the BlogPost model in order of most used to least used.