Home > Blockchain >  Django-taggit: how to retrieve all tags filtered by a certain type of posts
Django-taggit: how to retrieve all tags filtered by a certain type of posts

Time:10-04

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.

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!

CodePudding user response:

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.

Hope it helps.

  • Related