Django Taggit


  1. Tagging models with existing tags:

from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(Post)
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
Post.objects.filter(tags__in=tags, content_type=content_type)
  1. Creating new tags on the fly:

tag1 = Tag.objects.get_or_create(name='tag1')[0]
tag2 = Tag.objects.get_or_create(name='tag2')[0]
Post.objects.filter(tags__in=[tag1, tag2])
  1. Retrieving all tags associated with a model:

Post.objects.get(id=1).tags.all()
  1. Retrieving all tags associated with a specific object:

post = Post.objects.get(id=1)
post.tags.all()
  1. Adding tags to an object:

post = Post.objects.get(id=1)
tag1 = Tag.objects.get_or_create(name='tag1')[0]
tag2 = Tag.objects.get_or_create(name='tag2')[0]
post.tags.add(tag1, tag2)
  1. Removing tags from an object:

  1. Tagging objects in bulk:

  1. Retrieving objects with a specific tag:

  1. Retrieving objects with any of a list of tags:

  1. Retrieving objects with all of a list of tags:

  1. Retrieving objects with none of a list of tags:

  1. Creating a cloud of tags:

  1. Retrieving tags associated with a user:

  1. Adding tags to a user:

  1. Removing tags from a user:

  1. Retrieving users with a specific tag:

  1. Retrieving users with any of a list of tags:

  1. Retrieving users with all of a list of tags:

  1. Retrieving users with none of a list of tags:

  1. Tagging comments:

  1. Retrieving comments with a specific tag:

  1. Retrieving comments with any of a list of tags:

  1. Retrieving comments with all of a list of tags:

  1. Retrieving comments with none of a list of tags:

  1. Tagging files:

  1. Retrieving files with a specific tag:

  1. Retrieving files with any of a list of tags:

  1. Retrieving files with all of a list of tags:

  1. Retrieving files with none of a list of tags:

  1. Tagging custom models:

  1. Retrieving custom models with a specific tag:

  1. Retrieving custom models with any of a list of tags:

  1. Retrieving custom models with all of a list of tags:

  1. Retrieving custom models with none of a list of tags:

  1. Translating tags:

  1. Customizing the tag model: