[英]Django-taggit: how to retrieve all tags filtered by a certain type of posts
I'm building a blog, and I have two status for a post, Published and a Draft.我正在建立一个博客,我的帖子有两种状态,已发布和草稿。 I wanto to display all tags of all published posts using Django-taggit.
我想使用 Django-taggit 显示所有已发布帖子的所有标签。
here's how a get all tags for all kind of posts, Published and Draft in my view:在我看来,这是获取所有类型帖子、已发布和草稿的所有标签的方法:
object_list = Post.published.all()
tags = Tag.objects.filter()
And I want to get only tags for published posts我只想获得已发布帖子的标签
I got stuck, help!我卡住了,求救!
You could do something like this你可以这样做
inner_qs = Post.published.all().values('tags')
tags = Tag.objects.filter(id__in=inner_qs)
You should replace the tags
value for your actual field name for the tag, and the same thing with the id
field in the tag model in the id__in
filter.您应该将
tags
值替换为标签的实际字段名称,并在id__in
过滤器中将标签 model 中的id
字段替换为相同的值。
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.