如何使用 django-tagging 过滤标签?

发布于 2024-08-12 09:19:34 字数 571 浏览 1 评论 0原文

我正在使用 django 应用程序 django-tagging,并尝试过滤掉某些标签以进行简单的标签搜索。

变量“标签”是我正在搜索的某个标签的文本。 “Widget”是与标签关联的模型。

tags = Tag.objects.usage_for_model(Widget, counts=True, filters=dict(tags__icontains=tag))

上面的代码可以工作。它返回包含该标签的标签列表,但它也返回与使用该标签的小部件关联的其他标签。

例如,我有一个小部件:A,A有标签:django、python、mysql。 如果我对“django”进行简单搜索:

tags = Tag.objects.usage_for_model(Widget, counts=True, filters=dict(tags__icontains='django'))

标签将返回此列表:[(标签:django),(标签:python),(标签:mysql)]

我只希望它返回:[(标签:django)]

我该怎么做?

I'm using the django app django-tagging and I'm trying to filter out certain tags for a simple tag search.

the variable 'tag' is text of some tag I am searching for.
'Widget' is the model associated with the tags.

tags = Tag.objects.usage_for_model(Widget, counts=True, filters=dict(tags__icontains=tag))

The code above sort of works. It returns a list of tags which contain the tag, but it also returns other tags associated with widgets that use that tag.

For example, I have a widget: A, and A has tags: django, python, mysql.
If I do a simple search for 'django':

tags = Tag.objects.usage_for_model(Widget, counts=True, filters=dict(tags__icontains='django'))

tags will return this list: [(Tag: django), (Tag: python), (Tag: mysql)]

I only want it to return: [(Tag: django)]

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2024-08-19 09:19:34

迟到总比不到好?

djangoTag = Tag.objects.get(name='django')
djangoWidgets = TaggedItem.objects.get_union_by_model(Widget, djangoTag)

Better late than never?

djangoTag = Tag.objects.get(name='django')
djangoWidgets = TaggedItem.objects.get_union_by_model(Widget, djangoTag)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文