# Django Taggit

***

1. **Tagging models with existing tags:**

```python
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)
```

2. **Creating new tags on the fly:**

```python
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])
```

3. **Retrieving all tags associated with a model:**

```python
Post.objects.get(id=1).tags.all()
```

4. **Retrieving all tags associated with a specific object:**

```python
post = Post.objects.get(id=1)
post.tags.all()
```

5. **Adding tags to an object:**

```python
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)
```

6. **Removing tags from an object:**

```python
post = Post.objects.get(id=1)
tag1 = Tag.objects.get_or_create(name='tag1')[0]
post.tags.remove(tag1)
```

7. **Tagging objects in bulk:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
Post.objects.filter(pk__in=[1, 2, 3]).update(tags=tags)
```

8. **Retrieving objects with a specific tag:**

```python
Tag.objects.get(name='tag1').objects.all()
```

9. **Retrieving objects with any of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
Post.objects.filter(tags__in=tags)
```

10. **Retrieving objects with all of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
Post.objects.filter(tags__in=tags).distinct()
```

11. **Retrieving objects with none of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
Post.objects.exclude(tags__in=tags)
```

12. **Creating a cloud of tags:**

```python
from django.db.models import Count

tags = Tag.objects.annotate(num_posts=Count('post')).order_by('-num_posts')
```

13. **Retrieving tags associated with a user:**

```python
from django.contrib.auth.models import User

user = User.objects.get(username='john')
tags = user.taggit_tags.all()
```

14. **Adding tags to a user:**

```python
from django.contrib.auth.models import User

user = User.objects.get(username='john')
tag1 = Tag.objects.get_or_create(name='tag1')[0]
tag2 = Tag.objects.get_or_create(name='tag2')[0]
user.taggit_tags.add(tag1, tag2)
```

15. **Removing tags from a user:**

```python
from django.contrib.auth.models import User

user = User.objects.get(username='john')
tag1 = Tag.objects.get_or_create(name='tag1')[0]
user.taggit_tags.remove(tag1)
```

16. **Retrieving users with a specific tag:**

```python
from django.contrib.auth.models import User

tag = Tag.objects.get_or_create(name='tag1')[0]
users = User.objects.filter(taggit_tags__in=[tag])
```

17. **Retrieving users with any of a list of tags:**

```python
from django.contrib.auth.models import User

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
users = User.objects.filter(taggit_tags__in=tags)
```

18. **Retrieving users with all of a list of tags:**

```python
from django.contrib.auth.models import User

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
users = User.objects.filter(taggit_tags__in=tags).distinct()
```

19. **Retrieving users with none of a list of tags:**

```python
from django.contrib.auth.models import User

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
users = User.objects.exclude(taggit_tags__in=tags)
```

20. **Tagging comments:**

```python
from django.contrib.comments.models import Comment

comment = Comment.objects.get(id=1)
tag1 = Tag.objects.get_or_create(name='tag1')[0]
tag2 = Tag.objects.get_or_create(name='tag2')[0]
comment.taggit_tags.add(tag1, tag2)
```

21. **Retrieving comments with a specific tag:**

```python
from django.contrib.comments.models import Comment

tag = Tag.objects.get_or_create(name='tag1')[0]
comments = Comment.objects.filter(taggit_tags__in=[tag])
```

22. **Retrieving comments with any of a list of tags:**

```python
from django.contrib.comments.models import Comment

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
comments = Comment.objects.filter(taggit_tags__in=tags)
```

23. **Retrieving comments with all of a list of tags:**

```python
from django.contrib.comments.models import Comment

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
comments = Comment.objects.filter(taggit_tags__in=tags).distinct()
```

24. **Retrieving comments with none of a list of tags:**

```python
from django.contrib.comments.models import Comment

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
comments = Comment.objects.exclude(taggit_tags__in=tags)
```

25. **Tagging files:**

```python
from django.core.files.storage import default_storage

file = default_storage.open('path/to/file.txt')
tag1 = Tag.objects.get_or_create(name='tag1')[0]
tag2 = Tag.objects.get_or_create(name='tag2')[0]
file.tags.add(tag1, tag2)
```

26. **Retrieving files with a specific tag:**

```python
from django.core.files.storage import default_storage

tag = Tag.objects.get_or_create(name='tag1')[0]
files = default_storage.files_with_tag(tag)
```

27. **Retrieving files with any of a list of tags:**

```python
from django.core.files.storage import default_storage

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
files = default_storage.files_with_tags(tags)
```

28. **Retrieving files with all of a list of tags:**

```python
from django.core.files.storage import default_storage

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
files = default_storage.files_with_tags(tags, all=True)
```

29. **Retrieving files with none of a list of tags:**

```python
from django.core.files.storage import default_storage

tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
files = default_storage.files_without_tags(tags)
```

30. **Tagging custom models:**

```python
from django.db import models

class MyModel(models.Model):
    tags = TaggableManager()
    # ...
```

31. **Retrieving custom models with a specific tag:**

```python
MyModel.objects.filter(tags__name='tag1')
```

32. **Retrieving custom models with any of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
MyModel.objects.filter(tags__in=tags)
```

33. **Retrieving custom models with all of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
MyModel.objects.filter(tags__in=tags).distinct()
```

34. **Retrieving custom models with none of a list of tags:**

```python
tags = Tag.objects.filter(name__in=['tag1', 'tag2'])
MyModel.objects.exclude(tags__in=tags)
```

35. **Translating tags:**

```python
from django.utils.translation import ugettext_lazy as _

class Post(models.Model):
    tags = TaggableManager(verbose_name=_('tags'))
    # ...
```

36. **Customizing the tag model:**

```python
from django.contrib.contenttypes.fields import GenericRelation

class CustomTag(models.Model):
    name = models.CharField(max_length=255, unique=True)
    description = models.TextField(blank=True)
    objects = CustomTagManager()

class TaggedItem(models.Model):
    tag = models.ForeignKey(CustomTag, on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericRelation()
```
