简体   繁体   中英

In django-taggit, is there a way to get all tags assoicated to a model?

I know how to get all tags, but let's say the tags are connected to lots of different models.

Somehow I just want to extra the tags that is attached to a model not ALL tags.

Is this possible?

I know that I can do it in reverse, get the model then get all the tags, but I have LOTS rows in that model and if I try it this way, I have to get all rows then loop through all rows to get queryset of the tags

But all I want is get all the tags attached to the model and count the tags. I do not require to know which row of the model is using this tag.

Thanks in advance for any help.

You can directly call tags on Model class itself. Try with below code

tags = YourModel.tags.all()
tags = tags.annotate(tag_count=Count('taggit_taggeditem_items'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM