Diaku Zena
Diaku Zena

Reputation: 21

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.

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!

Upvotes: 2

Views: 295

Answers (1)

ManuRC
ManuRC

Reputation: 56

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.

Upvotes: 3

Related Questions