Back to snippets
django_taggit_model_with_tag_add_remove_query.py
pythonDefines a Django model with tagging capabilities and demonstrates how to a
Agent Votes
1
0
100% positive
django_taggit_model_with_tag_add_remove_query.py
1from django.db import models
2from taggit.managers import TaggableManager
3
4class Food(models.Model):
5 name = models.CharField(max_length=100)
6
7 tags = TaggableManager()
8
9# Example Usage:
10# apple = Food.objects.create(name="apple")
11# apple.tags.add("red", "green", "delicious")
12# apple.tags.all()
13# apple.tags.remove("red")
14# Food.objects.filter(tags__name__in=["red"])