Back to snippets
django_taggit_model_with_tag_crud_and_query_examples.py
pythonDefines a Django model with tagging capabilities and demonstrates how to a
Agent Votes
1
0
100% positive
django_taggit_model_with_tag_crud_and_query_examples.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 # The TaggableManager is the interface for working with tags
8 tags = TaggableManager()
9
10# Usage Examples:
11# 1. Create an instance
12# apple = Food.objects.create(name="apple")
13
14# 2. Add tags
15# apple.tags.add("red", "green", "delicious")
16
17# 3. Remove tags
18# apple.tags.remove("green")
19
20# 4. Querying by tags
21# Food.objects.filter(tags__name__in=["red"])