Search code examples
pythondjangodjango-modelsdjango-taggit

Get all tags from taggit


How to get all the (unique) tags from django-taggit? I would like to display all the tags in a side bar. Currently I am able to get all the tags for a particular post, but now I need to get all the unique tags in the entire blog.

code in models.py:

from django.db import models
from taggit.managers import TaggableManager

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    created = models.DateTimeField()
    tags = TaggableManager()

Solution

  • You can use all() to get all the tags in your database:

    from taggit.models import Tag
    tags = Tag.objects.all()
    

    If you need a complete solution, have a look at django-taggit-templatetags. It provides several templatetags, including one for tag list, to expose various taggit APIs directly to templates.