与简单的 ManyToManyField() 标记实现相比,Django-Taggit 有什么好处?
根据文档的 API 似乎可以通过简单的 ManyToManyField 来实现......我错过了什么?
Django-Taggit 文档中的示例:
class Food(models.Model):
# ... fields here
tags = TaggableManager()
然后您可以像这样使用 API::
>>> apple = Food.objects.create(name="apple")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
[<Tag: red>, <Tag: green>, <Tag: delicious>]
>>> apple.tags.remove("green")
>>> apple.tags.all()
[<Tag: red>, <Tag: delicious>]
>>> Food.objects.filter(tags__name__in=["red"])
[<Food: apple>, <Food: cherry>]
The API according to the documentation seems achievable with a simple ManyToManyField...what am I missing?
Sample from Django-Taggit documentation:
class Food(models.Model):
# ... fields here
tags = TaggableManager()
Then you can use the API like so::
>>> apple = Food.objects.create(name="apple")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
[<Tag: red>, <Tag: green>, <Tag: delicious>]
>>> apple.tags.remove("green")
>>> apple.tags.all()
[<Tag: red>, <Tag: delicious>]
>>> Food.objects.filter(tags__name__in=["red"])
[<Food: apple>, <Food: cherry>]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真正的优势不在于找到对象的标签,而在于找到标签的对象。具体来说,如果您有多种类型的可以标记的对象,想象一下:
现在找到标记为“紫色”的对象的所有实例。 Taggit 让这一切变得更加容易。
The real advantage is not in finding the tags of an object, but rather the objects for a tag. And specifically, if you have multiple types of objects that can be tagged, imagine:
Now find me all the instances of objects tagged "purple". Taggit makes it a lot easier to do so.